cut
— column concatenation
The cut
tool can be used to extract specific columns from files
which are delimited by a particular character or by column numbers. It
can be passed filenames on the commandline; if none are specified, it
reads from stdin.
The cut
tool considers the first character in a line to have
index 1
. The -c
, -f
and -b
switches take a
parameter listing the desired columns. This can be a single value, or
a more complex list of values separated by commas. Each value can be a
single number, or two numbers separated by hyphens representing
low-high
. If low
is unspecified, it is treated as the
first column. If high
is unspecified, it is treated as being
"up to the last character (inclusive)".
To select particular characters from each line, use the -c
switch. For particular bytes (not the same as characters when using
multibyte text), use -b
. To specify particular fields,
use -f
.
When using -f
, the field delimiter can be specified using the
-d
switch. The default value is the tab character. The
-s
switch instructs cut
to suppress lines which do not
contain any instances of the delimiter — by default they are echoed
intact.
For example, to extract the second, fourth and fifth columns in a comma-delimited file, ignoring lines which contain no commas, one could use:
cut -s -d , -f 2,4-5 input.txt > output.txt
To chop the first character off stdin, one could use:
do_stuff | cut -c 2-
See the cut(1)
manpage and
IEEE Std 1003.1-2017-cut for full documentation.