{{lowercase}}{{For|the more common operation related to copying and pasting|Cut, copy, and paste}}{{Infobox Software | name = cut | logo = | screenshot = | screenshot size = | caption = | author = | developer = | released = {{Start date and age|1985|2}} | latest release version = | latest release date = | operating system = Unix and Unix-like | genre = Command | license = GNU GPL v3 | website = }}
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 (-b), characters (-c), or fields (-f) separated by a delimiter (-d — the tab character by default). A range must be provided in each case which consists of one of N, N-M,N- (N to the end of the line), or -M (beginning of the line to M), where N and M are counted from 1 (there is no zeroth value). 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
The command first appeared in AT&T System III UNIX in 1982.[1]{{better source|date=February 2015}}
The version of cut bundled in GNU coreutils was written by David M. Ihnat, David MacKenzie, and Jim Meyering.[2]
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:
To output the fifth field through the end of the line of each line using the colon character as the field delimiter:
(note that because the colon character is not found in the last line the entire line is shown)
Option -d specified a single character delimiter (in the example above it is a colon) which serves as field separator. Option -f which specifies range of fields included in the output (here fields range from five till the end). Option -d presupposes usage of option -f.
To output the third field of each line using space as the field delimiter:
(Note that because the space character is not found in the first three lines these entire lines are shown.)
To separate two words having any delimiter:
Syntax
Flags which may be used include
{{mono|-b}}
Bytes; a list following {{mono|-b}} specifies a range of bytes which will be returned, e.g. {{code|cut -b1-66}} would return the first 66 bytes of a line. NB If used in conjunction with {{mono|-n}}, no multi-byte characters will be split. NNB. {{mono|-b}} will only work on input lines of less than 1023 bytes
{{mono|-c}}
Characters; a list following {{mono|-c}} specifies a range of characters which will be returned, e.g. {{code|cut -c1-66}} would return the first 66 characters of a line
{{mono|-f}}
Specifies a field list, separated by a delimiter
list
A comma separated or blank separated list of integer denoted fields, incrementally ordered. The {{mono|-}} indicator may be supplied as shorthand to allow inclusion of ranges of fields e.g. {{mono|4-6}} for ranges 4–6 or {{mono|5-}} as shorthand for field 5 to the end, etc.
{{mono|-n}}
Used in combination with -b suppresses splits of multi-byte characters
{{mono|-d}}
Delimiter; the character immediately following the {{mono|-d}} option is the field delimiter for use in conjunction with the {{mono|-f}} 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.
{{mono|-s}}
Bypasses lines which contain no field delimiters when {{mono|-f}} is specified, unless otherwise indicated.
file
The file (and accompanying path if necessary) to process as input. If no file is specified then standard input will be used.
See also
List of Unix commands
grep
paste
sed
awk
References
1. ^{{cite web|url=http://www.unix.com/man-page/FreeBSD/1/CUT/|title=FreeBSD 11.0 - man page for cut (freebsd section 1) - Unix & Linux Commands|author=|date=|website=www.unix.com|accessdate=18 April 2018}} 2. ^https://linux.die.net/man/1/cut
External links
Softpanorama cut page.
{{man|cu|cut|SUS}}
Cut out selected fields of each line of a file A portrait of cut(1) and its historical background.
{{Unix commands}}
3 : Unix text processing utilities|Standard Unix programs|Unix SUS2008 utilities