| cut | |
|---|---|
| Original author | AT&T Bell Laboratories |
| Developers | Variousopen-source andcommercial developers |
| Initial release | February 1985; 41 years ago (1985-02) |
| Operating system | Unix,Unix-like,IBM i |
| Platform | Cross-platform |
| Type | Command |
| License | coreutils:GPLv3+ |
cut is ashellcommand that extracts sections from each line of input text — usually from afile. Extraction of line segments can typically be done bybytes (-b),characters (-c), or fields (-f) separated by a delimiter (-d — thetab character by default). A range must be provided in each case which consists of one ofN,N-M,N- (N to the end of the line), or-M (beginning of the line toM), 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.
The originalBell Labs version was written by Gottfried W. R. Luderer.[1][2] The command is part of theX/Open Portability Guide since issue 2 of 1987. It was inherited into the first version of POSIX.1 and theSingle Unix Specification.[3] It first appeared inAT&T System III UNIX in 1982.[4]
The command is commonly available onUnix andUnix-likeoperating systems. It is part of theBSD Base System. The version inGNUcoreutils was written by David M. Ihnat, David MacKenzie, and Jim Meyering.[5] The command is available forWindows viaUnxUtils.[6] The command was ported to theIBM i operating system.[7]
Thecommand line consists of options and an optional file path. If no path is specified thanstandard input will be used.
Options include:
cut -b1-66 would return the first 66 bytes of a line. NB If used in conjunction with-n, no multi-byte characters will be split. NNB.-b will only work on input lines of less than 1023 bytescut -c1-66 would return the first 66 characters of a lineGiven a file namedfoo with content:
foo:bar:baz:qux:quuxone:two:three:four:five:six:sevenalpha:beta:gamma:delta:epsilon:zeta:eta:theta:iota:kappa:lambda:muthe quick brown fox jumps over the lazy dog
To output the fourth through tenth characters of each line:
$cut-c4-10foo:bar:ba:two:thha:beta quick
To output the fifth field through the end of the line of each line using thecolon character as the field delimiter:
$cut-d":"-f5-fooquuxfive:six:sevenepsilon:zeta:eta:theta:iota:kappa:lambda:muthe quick brown fox jumps over the lazy dog
Because the colon is not found in the last line, the entire line is shown.
Option-d specifies 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:
$cut-d" "-f3foofoo:bar:baz:qux:quuxone:two:three:four:five:six:sevenalpha:beta:gamma:delta:epsilon:zeta:eta:theta:iota:kappa:lambda:mubrown
Because the space character is not found in the first three lines these entire lines are shown.
To separate two words having any delimiter:
$line=process.processid$cut-d"."-f1<<<$lineprocess$cut-d"."-f2<<<$lineprocessid
cut – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Groupcut(1) – FreeBSD General CommandsManualcut(1) – Linux General CommandsManual from ManKier.comcut – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Group