Cut (Unix)


In computing, cut is a Unix command line utility which is used to extract sections from each line of input — usually from a file. It is currently part of the GNU coreutils package and the BSD Base System.
Extraction of line segments can typically be done by bytes, characters, or fields separated by a delimiter. A range must be provided in each case which consists of one of N, N-M, N-, or -M, where N and M are counted from 1. Since version 6, an error is thrown if you include a zeroth value. Prior to this the value was ignored and assumed to be 1.

History

is part of the X/Open Portability Guide since issue 2 of 1987. It was inherited into the first version of POSIX.1 and the Single Unix Specification. It first appeared in AT&T System III UNIX in 1982.
The version of cut bundled in GNU coreutils was written by David M. Ihnat, David MacKenzie, and Jim Meyering.

Examples

Assuming a file named "file" containing the lines:
foo:bar:baz:qux:quux
one:two:three:four:five:six:seven
alpha:beta:gamma:delta:epsilon:zeta:eta:theta:iota:kappa:lambda:mu
the quick brown fox jumps over the lazy dog
To output the fourth through tenth characters of each line:

$ cut -c 4-10 file
ha:beta
quick

To output the fifth field through the end of the line of each line using the colon character as the field delimiter:

$ cut -d ":" -f 5- file
quux
five:six:seven
epsilon:zeta:eta:theta:iota:kappa:lambda:mu
the quick brown fox jumps over the lazy dog

Option -d specified a single character delimiter which serves as field separator. Option -f which specifies range of fields included in the output. Option -d presupposes usage of option -f.
To output the third field of each line using space as the field delimiter:

$ cut -d " " -f 3 file
foo:bar:baz:qux:quux
one:two:three:four:five:six:seven
alpha:beta:gamma:delta:epsilon:zeta:eta:theta:iota:kappa:lambda:mu
brown

To separate two words having any delimiter:

$ line=process.processid
$ cut -d "." -f1 <<< $line
process
$ cut -d "." -f2 <<< $line
processid

Syntax

cut
Flags which may be used include:
; : Bytes; a list following specifies a range of bytes which will be returned, e.g. would return the first 66 bytes of a line. NB If used in conjunction with, no multi-byte characters will be split. NNB. will only work on input lines of less than 1023 bytes
; : Characters; a list following specifies a range of characters which will be returned, e.g. would return the first 66 characters of a line
; : Specifies a field list, separated by a delimiter
; list : A comma separated or blank separated list of integer denoted fields, incrementally ordered. The indicator may be supplied as shorthand to allow inclusion of ranges of fields e.g. for ranges 4–6 or as shorthand for field 5 to the end, etc.
; : Used in combination with -b suppresses splits of multi-byte characters
; : Delimiter; the character immediately following the option is the field delimiter for use in conjunction with the option; the default delimiter is tab. Space and other characters with special meanings within the context of the shell in use must be enquoted or escaped as necessary.
; : Bypasses lines which contain no field delimiters when is specified, unless otherwise indicated.
; file : The file to process as input. If no file is specified then standard input will be used.