Intro To 'cut' Command In Linux
2023-10-18 - By Robert Elder
I use the cut command to cut columns out of text documents. For example, in the file 'foo.txt'
cat foo.txt
a 1
b 2
c 3
d 4
e 5
I can extract the first column like this:
cut -f 1 foo.txt
a
b
c
d
e
or the second column like this:
cut -f 2 foo.txt
1
2
3
4
5
A Terminal-like Spreadsheet Cut Tool
The cut command could be thought of as a primitive terminal based version of performing a 'cut' operation inside of a spreadsheet program:
Extracting One Or More Columns
Here, I have a text file called 'books.txt' that contains a list of books in tab separated column format:
Tropic of Cancer Henry Miller 1934
Housekeeping Marilynne Robinson 1981
Deliverance James Dickey 1970
The Sun Also Rises Ernest Hemingway 1926
The Great Gatsby F. Scott Fitzgerald 1925
The Corrections Jonathan Franzen 2001
The Berlin Stories Christopher Isherwood 1946
Call It Sleep Henry Roth 1935
Slaughterhouse-Five Kurt Vonnegut 1969
Light in August William Faulkner 1932
I can use the cut command with the '-f' flag to extract each column individually by it's one-based index number:
cut -f 1 books.txt
Tropic of Cancer
Housekeeping
Deliverance
The Sun Also Rises
The Great Gatsby
The Corrections
The Berlin Stories
Call It Sleep
Slaughterhouse-Five
Light in August
cut -f 2 books.txt
Henry Miller
Marilynne Robinson
James Dickey
Ernest Hemingway
F. Scott Fitzgerald
Jonathan Franzen
Christopher Isherwood
Henry Roth
Kurt Vonnegut
William Faulkner
cut -f 3 books.txt
1934
1981
1970
1926
1925
2001
1946
1935
1969
1932
I can even extract multiple columns at once:
cut -f 1,3 books.txt
Tropic of Cancer 1934
Housekeeping 1981
Deliverance 1970
The Sun Also Rises 1926
The Great Gatsby 1925
The Corrections 2001
The Berlin Stories 1946
Call It Sleep 1935
Slaughterhouse-Five 1969
Light in August 1932
Using 'awk' As Alternative
Most of the use cases for the '-f' flag are made redundant by the 'awk' command which is more powerful:
cat books.txt | awk -F'\t' '{print $1"\t"$3}'
Tropic of Cancer 1934
Housekeeping 1981
Deliverance 1970
The Sun Also Rises 1926
The Great Gatsby 1925
The Corrections 2001
The Berlin Stories 1946
Call It Sleep 1935
Slaughterhouse-Five 1969
Light in August 1932
This is even pointed out in the 'cut' command's own documentation:
info cut
...
Note ‘awk’ supports more sophisticated field processing, like
reordering fields, and handling fields aligned with blank
characters. By default ‘awk’ uses (and discards) runs of blank
characters to separate fields, and ignores leading and trailing
blanks.
awk '{print $2}' # print the second field
awk '{print $(NF-1)}' # print the penultimate field
awk '{print $2,$1}' # reorder the first two fields
Note while ‘cut’ accepts field specifications in arbitrary order,
output is always in the order encountered in the file.
In the unlikely event that ‘awk’ is unavailable, one can use the
‘join’ command, to process blank characters as ‘awk’ does above.
join -a1 -o 1.2 - /dev/null # print the second field
join -a1 -o 1.2,1.1 - /dev/null # reorder the first two fields
...
Cut Columns By Byte Offsets
The '-b' flag can be used to extract columns based on byte offsets. Here, I have a text document named 'model-numbers.txt' that contains model numbers of products:
US-AXA-4066-2006
US-CBB-4174-2006
ER-BYA-1760-2015
KM-BDB-7051-2016
MT-ADB-2658-2003
US-BHA-3252-2002
KG-BAC-8635-2010
GT-CFB-2489-2002
FM-CEA-1980-2002
MZ-CDB-5351-2010
DM-CWA-8389-2020
BD-BBC-1912-2012
IO-CFB-6127-2018
LV-ACB-8268-2006
CM-BVB-1519-2015
US-AQB-3275-2006
The first two characters are the country code, and the last four characters are the year. I can use this cut command to extract the country code and the year together:
cut -b 1,2,3,13,14,15,16 model-numbers.txt
US-2006
US-2006
ER-2015
KM-2016
MT-2003
US-2002
KG-2010
GT-2002
FM-2002
MZ-2010
DM-2020
BD-2012
IO-2018
LV-2006
CM-2015
US-2006
And that's why the 'cut' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|