The cut
command is a fast way to extract parts of lines of text files. It belongs to the oldest Unix commands. Its most popular implementations are the GNU version found on Linux and the FreeBSD version found on MacOS, but each flavor of Unix has its own. See below for differences. The input lines are read either from stdin
or from files listed as arguments on the command line.
|Parameter|Details| |:—|:—| |-f, –fields| Field-based selection |-d, –delimiter| Delimiter for field-based selection |-c, –characters| Character-based selection, delimiter ignored or error |-s, –only-delimited| Suppress lines with no delimiter characters (printed as-is otherwise) |–complement| Inverted selection (extract all except specified fields/characters |–output-delimiter| Specify when it has to be different from the input delimiter
1. Syntax differences
Long options in the table above are only supported by the GNU version.
2. No character gets special treatment
FreeBSD cut
(which comes with MacOS, for example) doesn’t have the --complement
switch, and, in the case of character ranges, one can use the colrm
command instead:
$ cut --complement -c3-5 <<<"123456789"
126789
$ colrm 3 5 <<<"123456789"
126789
However, there is a big difference, because colrm
treats TAB characters (ASCII 9) as real tabulations up to the next multiple of eight, and backspaces (ASCII 8) as -1 wide; on the contrary, cut
treats all characters as one column wide.
$ colrm 3 8 <<<$'12\\tABCDEF' # Input string has an embedded TAB
12ABCDEF
$ cut --complement -c3-8 <<<$'12\\tABCDEF'
12F