Glossary

cut

cut is a minimal tool for extracting specific byte ranges, character ranges, or delimited fields from each line of its input. It is the quickest way to pull a column out of structured text without writing an awk script.

cut -d: -f1 /etc/passwd           # usernames (colon-delimited, field 1)
cut -d: -f1,7 /etc/passwd         # username and shell
cut -d$'\\t' -f2,4 file           # tab-delimited, fields 2 and 4
cut -c1-10 file                    # first 10 characters
ps aux | tr -s ' ' | cut -d' ' -f2 # pid column, with squeezed spaces

cut's main limitation is that it treats its delimiter literally: it cannot handle variable amounts of whitespace, which is why ps output often needs tr -s ' ' to squeeze spaces first. For that kind of thing, awk is far more convenient (ps aux | awk '{print $2}'), but cut is faster and reads more cleanly when the delimiter is fixed.

cut is one of those tools that makes one-liners feel economical. who | cut -d' ' -f1 | sort -u prints the unique set of users currently logged in, in three short commands chained together.

Related terms: awk, grep

Discussed in:

Also defined in: Textbook of Linux