The cut command

The cut command is most often used to select single columns of data from input separated by a single character, such as an /etc/passwd file. For example, this command line prints the home directories of every user on a GNU/Linux system:

$ cut -d: -f6 /etc/passwd
/root
/usr/sbin
/bin
...

In this example, -d specifies the delimiter or separator variable, in this case a colon, and -f specifies the number of the field (or column), starting from 1.

We can also specify the field numbers with hyphen-separated ranges, and/or comma-separated indices:

$ cut -d: -f1,6 /etc/passwd
root:/root
daemon:/usr/sbin
bin:/bin
...
$ cut -d: -f1,3-4 /etc/passwd
root:0:0
daemon:1:1
bin:2:2
...

We can leave one of the numbers out of a range, to mean ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.