Skip to content

Commit 38cdb01

Browse files
committed
doc: add alternatives for field processing not supported by cut
* doc/coreutils.texi (cut invocation): Remove the tr -s '[:blank:]' example, as it doesn't handle leading and trailing blanks. Add `awk` examples for common field processing operations often asked about. Also document a `join` hack, to achieve the same thing. Note the join options are ordered so as to be compatible with other systems.
1 parent 877ca5b commit 38cdb01

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

doc/coreutils.texi

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5506,11 +5506,26 @@ Select for printing only the fields listed in @var{field-list}.
55065506
Fields are separated by a TAB character by default. Also print any
55075507
line that contains no delimiter character, unless the
55085508
@option{--only-delimited} (@option{-s}) option is specified.
5509-
Note @command{cut} does not support specifying runs of whitespace as a
5510-
delimiter, so to achieve that common functionality one can pre-process
5511-
with @command{tr} like:
5509+
5510+
Note @command{awk} supports more sophisticated field processing,
5511+
and by default will use (and discard) runs of blank characters to
5512+
separate fields, and ignore leading and trailing blanks.
5513+
@example
5514+
@verbatim
5515+
awk '{print $2}' # print the second field
5516+
awk '{print $NF-1}' # print the penultimate field
5517+
awk '{print $2,$1}' # reorder the first two fields
5518+
@end verbatim
5519+
@end example
5520+
5521+
In the unlikely event that @command{awk} is unavailable,
5522+
one can use the @command{join} command, to process blank
5523+
characters as @command{awk} does above.
55125524
@example
5513-
tr -s '[:blank:]' '\t' | cut -f@dots{}
5525+
@verbatim
5526+
join -a1 -o 1.2 - /dev/null # print the second field
5527+
join -a1 -o 1.2,1.1 - /dev/null # reorder the first two fields
5528+
@end verbatim
55145529
@end example
55155530

55165531
@item -d @var{input_delim_byte}

0 commit comments

Comments
 (0)