cut: fix "echo 1.2 | cut -d. -f1,3" (print "1", not "1.")
function old new delta cut_main 1228 1201 -27 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
470f009552
commit
103139d0e6
2 changed files with 27 additions and 7 deletions
|
@ -114,7 +114,8 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(printed);
|
free(printed);
|
||||||
} else if (!opt_REGEX && *delim == '\n') { /* cut by lines */
|
/* Cut by lines */
|
||||||
|
} else if (!opt_REGEX && *delim == '\n') {
|
||||||
int spos = cut_list[cl_pos].startpos;
|
int spos = cut_list[cl_pos].startpos;
|
||||||
|
|
||||||
/* get out if we have no more lists to process or if the lines
|
/* get out if we have no more lists to process or if the lines
|
||||||
|
@ -144,7 +145,8 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
|
||||||
* looking for, so print it */
|
* looking for, so print it */
|
||||||
puts(line);
|
puts(line);
|
||||||
goto next_line;
|
goto next_line;
|
||||||
} else { /* cut by fields */
|
/* Cut by fields */
|
||||||
|
} else {
|
||||||
unsigned next = 0, start = 0, end = 0;
|
unsigned next = 0, start = 0, end = 0;
|
||||||
int dcount = 0; /* Nth delimiter we saw (0 - didn't see any yet) */
|
int dcount = 0; /* Nth delimiter we saw (0 - didn't see any yet) */
|
||||||
int first_print = 1;
|
int first_print = 1;
|
||||||
|
@ -159,22 +161,33 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
|
||||||
for (;;) {
|
for (;;) {
|
||||||
/* End of current range? */
|
/* End of current range? */
|
||||||
if (end == linelen || dcount > cut_list[cl_pos].endpos) {
|
if (end == linelen || dcount > cut_list[cl_pos].endpos) {
|
||||||
|
end_of_range:
|
||||||
if (++cl_pos >= nlists)
|
if (++cl_pos >= nlists)
|
||||||
break;
|
break;
|
||||||
if (option_mask32 & OPT_NOSORT)
|
if (option_mask32 & OPT_NOSORT)
|
||||||
start = dcount = next = 0;
|
start = dcount = next = 0;
|
||||||
end = 0; /* (why?) */
|
end = 0; /* (why?) */
|
||||||
|
//bb_error_msg("End of current range");
|
||||||
}
|
}
|
||||||
/* End of current line? */
|
/* End of current line? */
|
||||||
if (next == linelen) {
|
if (next == linelen) {
|
||||||
|
end = linelen; /* print up to end */
|
||||||
/* If we've seen no delimiters, check -s */
|
/* If we've seen no delimiters, check -s */
|
||||||
if (cl_pos == 0 && dcount == 0 && !opt_REGEX) {
|
if (cl_pos == 0 && dcount == 0 && !opt_REGEX) {
|
||||||
if (option_mask32 & OPT_SUPPRESS)
|
if (option_mask32 & OPT_SUPPRESS)
|
||||||
goto next_line;
|
goto next_line;
|
||||||
/* else: will print entire line */
|
/* else: will print entire line */
|
||||||
} else if (dcount < cut_list[cl_pos].startpos)
|
} else if (dcount < cut_list[cl_pos].startpos) {
|
||||||
start = linelen; /* do not print */
|
/* echo 1.2 | cut -d. -f1,3: prints "1", not "1." */
|
||||||
end = linelen; /* print up to end */
|
//break;
|
||||||
|
/* ^^^ this fails a case with -D:
|
||||||
|
* echo 1 2 | cut -DF 1,3,2:
|
||||||
|
* do not end line processing when didn't find field#3
|
||||||
|
*/
|
||||||
|
//if (option_mask32 & OPT_NOSORT) - no, just do it always
|
||||||
|
goto end_of_range;
|
||||||
|
}
|
||||||
|
//bb_error_msg("End of current line: s:%d e:%d", start, end);
|
||||||
} else {
|
} else {
|
||||||
/* Find next delimiter */
|
/* Find next delimiter */
|
||||||
#if ENABLE_FEATURE_CUT_REGEX
|
#if ENABLE_FEATURE_CUT_REGEX
|
||||||
|
|
|
@ -90,7 +90,14 @@ Subcalifragilisticexpialidocious.
|
||||||
Auntie Em: Hate you, hate Kansas. Took the dog. Dorothy."
|
Auntie Em: Hate you, hate Kansas. Took the dog. Dorothy."
|
||||||
SKIP=
|
SKIP=
|
||||||
|
|
||||||
testing "cut empty field" "cut -d ':' -f 1-3" "a::b\n" "" "a::b\n"
|
testing "cut empty field" "cut -d ':' -f 1-3" \
|
||||||
testing "cut empty field 2" "cut -d ':' -f 3-5" "b::c\n" "" "a::b::c:d\n"
|
"a::b\n" \
|
||||||
|
"" "a::b\n"
|
||||||
|
testing "cut empty field 2" "cut -d ':' -f 3-5" \
|
||||||
|
"b::c\n" \
|
||||||
|
"" "a::b::c:d\n"
|
||||||
|
testing "cut non-existing field" "cut -d ':' -f1,3" \
|
||||||
|
"1\n" \
|
||||||
|
"" "1:\n"
|
||||||
|
|
||||||
exit $FAILCOUNT
|
exit $FAILCOUNT
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue