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 |
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.
Only one delimiter character
You cannot have more than one delimiter: if you specify something like -d “,;:”, some implementations will use only the first character as a delimiter (in this case, the comma.) Other implementations (e.g. GNU cut) will give you an error message.
$ cut -d ",;:" -f2 <<<"J.Smith,1 Main Road,cell:1234567890;land:4081234567"
cut: the delimiter must be a single character
Try `cut --help' for more information.
Repeated delimiters are interpreted as empty fields
$ cut -d, -f1,3 <<<"a,,b,c,d,e"
a,b
is rather obvious, but with space-delimited strings it might be less obvious to some
$ cut -d ' ' -f1,3 <<<"a b c d e"
a b
cut cannot be used to parse arguments as the shell and other programs do.
No quoting
There is no way to protect the delimiter. Spreadsheets and similar CSV-handling software usually can recognize a text-quoting character which makes it possible to define strings containing a delimiter. With cut you cannot.
$ cut -d, -f3 <<<'John,Smith,"1, Main Street"'
"1
Extracting, not manipulating
You can only extract portions of lines, not reorder or repeat fields.
$ cut -d, -f2,1 <<<'John,Smith,USA' ## Just like -f1,2
John,Smith
$ cut -d, -f2,2 <<<'John,Smith,USA' ## Just like -f2