Cut (Unix)
Shell command for extracting sections of text files
- 3 min read
- 3 chapters
Related on Lably
Through Command (computing), Character (computing) and Operating system
History
The original Bell Labs version was written by Gottfried W. R. Luderer. The command 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 command is commonly available on Unix and Unix-like operating systems. It is part of the BSD Base System. The version in GNU coreutils was written by David M. Ihnat, David MacKenzie, and Jim Meyering. The command is available for Windows via UnxUtils. The command was ported to the IBM i operating system.
Sign in to keep reading
2 more chapters are free with a Lably account.
Use
The command line consists of options and an optional file path. If no path is specified than standard input will be used.
Options include:
- -b
- Bytes; a list following -b specifies a range of bytes which will be returned, e.g.
cut -b1-66would return the first 66 bytes of a line. NB If used in conjunction with -n, no multi-byte characters will be split.
- -c
- Characters; a list following -c specifies a range of characters which will be returned, e.g.
cut -c1-66would return the first 66 characters of a line
- -f
- 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. 4-6 for ranges 4–6 or 5- as shorthand for field 5 to the end, etc.
- -n
- Used in combination with -b suppresses splits of multi-byte characters
- -d
- Delimiter; the character immediately following the -d option is the field delimiter for use in conjunction with the -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.
- -s
- Bypasses lines which contain no field delimiters when -f is specified, unless otherwise indicated.
Examples
Given a file named foo with content:
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:
<span id="mwjQ"></span><span class="gp" id="mwjg">$ </span>cut<span class="w" id="mwjw"> </span>-c<span class="w" id="mwkA"> </span><span class="m" id="mwkQ">4</span>-10<span class="w" id="mwkg"> </span>foo <span class="go" id="mwkw">:bar:ba</span> <span class="go" id="mwlA">:two:th</span> <span class="go" id="mwlQ">ha:beta</span> <span class="go" id="mwlg"> quick</span>
To output the fifth field through the end of the line of each line using the colon character as the field delimiter:
<span id="mwmw"></span><span class="gp" id="mwnA">$ </span>cut<span class="w" id="mwnQ"> </span>-d<span class="w" id="mwng"> </span><span class="s2" id="mwnw">":"</span><span class="w" id="mwoA"> </span>-f<span class="w" id="mwoQ"> </span><span class="m" id="mwog">5</span>-<span class="w" id="mwow"> </span>foo <span class="go" id="mwpA">quux</span> <span class="go" id="mwpQ">five:six:seven</span> <span class="go" id="mwpg">epsilon:zeta:eta:theta:iota:kappa:lambda:mu</span> <span class="go" id="mwpw">the quick brown fox jumps over the lazy dog</span>
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:
<span id="mwsQ"></span><span class="gp" id="mwsg">$ </span>cut<span class="w" id="mwsw"> </span>-d<span class="w" id="mwtA"> </span><span class="s2" id="mwtQ">" "</span><span class="w" id="mwtg"> </span>-f<span class="w" id="mwtw"> </span><span class="m" id="mwuA">3</span><span class="w" id="mwuQ"> </span>foo <span class="go" id="mwug">foo:bar:baz:qux:quux</span> <span class="go" id="mwuw">one:two:three:four:five:six:seven</span> <span class="go" id="mwvA">alpha:beta:gamma:delta:epsilon:zeta:eta:theta:iota:kappa:lambda:mu</span> <span class="go" id="mwvQ">brown</span>
Because the space character is not found in the first three lines these entire lines are shown.
To separate two words having any delimiter:
<span id="mwwg"></span><span class="gp" id="mwww">$ </span><span class="nv" id="mwxA">line</span><span class="o" id="mwxQ">=</span>process.processid <span class="gp" id="mwxg">$ </span>cut<span class="w" id="mwxw"> </span>-d<span class="w" id="mwyA"> </span><span class="s2" id="mwyQ">"."</span><span class="w" id="mwyg"> </span>-f1<span class="w" id="mwyw"> </span><span class="o" id="mwzA"><<<</span><span class="w" id="mwzQ"> </span><span class="nv" id="mwzg">$line</span> <span class="go" id="mwzw">process</span> <span class="gp" id="mw0A">$ </span>cut<span class="w" id="mw0Q"> </span>-d<span class="w" id="mw0g"> </span><span class="s2" id="mw0w">"."</span><span class="w" id="mw1A"> </span>-f2<span class="w" id="mw1Q"> </span><span class="o" id="mw1g"><<<</span><span class="w" id="mw1w"> </span><span class="nv" id="mw2A">$line</span> <span class="go" id="mw2Q">processid</span>

