Unix Cut Command
Unix Cut Command
The cut command takes a vertical slice of a file, printing only the specified
columns or fields. Like the sort command, the cut command defines a field as a word
set off by blanks, unless you specify your own delimiter. It's easiest to think of
a column as just the nth character on each line. In other words, "column 5"
consists of the fifth character of each line.
Consider a slight variation on the company.data file we've been playing with in
this section:
406378:Sales:Itorre:Jan
031762:Marketing:Nasium:Jim
636496:Research:Ancholie:Mel
396082:Sales:Jucacion:Ed
If you want to print just columns 1 to 6 of each line (the employee serial
numbers), use the -c1-6 flag, as in this command:
If you want to print just columns 4 and 8 of each line (the first letter of the
department and the fourth digit of the serial number), use the -c4,8 flag, as in
this command:
And since this file obviously has fields delimited by colons, we can pick out just
the last names by specifying the -d: and -f3 flags, like this:
It's often the case that you want to use a space as the delimiter. To do so, you
must put the delimiter in single quotes, like this: -d' '
Also, when you want to cut from a starting point to the end of the line, just leave
off the final field number, as shown in the example below.
To cut only columns 2-END, do this: cut -d' ' -f2- test.txt
Here is a summary of the most common flags for the cut command: