cut

cut extracts columns from text. For each line of input it prints only the parts you select, dropping the rest.

cut option... [file...]
$ cut -d , -f 1,3 people.csv     # the 1st and 3rd comma-separated field
$ cut -c 1-8 log.txt             # the first 8 characters of each line

Every run of cut needs two decisions: what counts as a column, and which columns to keep.

What counts as a column #

Choose exactly one mode:

OptionA column is…
-b, --bytesa single byte.
-c, --charactersa single character.
-f, --fieldsa field — a stretch of text between delimiters.

-b and -c cut by position. -f cuts by field, which is what you want for tabular data — a CSV, a table, the columns of a report.

Which columns to keep #

The mode option takes a sequence: numbers and inclusive ranges, separated by commas.

SequenceSelects
3column 3 only.
2,5,9columns 2, 5, and 9.
5-7columns 5 through 7.
3-column 3 to the end of the line.
-4the start of the line through column 4.
1,4-6,9any mixture of the above.
OptionEffect
--complementInvert the selection — keep every column except those named.

Field mode: the delimiter #

In field mode, cut needs to know what separates the fields.

OptionEffect
-d, --delimiter=CHARThe character that separates fields. The default is a tab.
-wSeparate fields on runs of whitespace (spaces and tabs) instead of a single character. Cannot be combined with -d.
-s, --only-delimitedPrint only lines that actually contain the delimiter; drop lines that have no fields to cut.
--output-delimiter=STRINGPut STRING between the kept fields in the output, instead of repeating the input delimiter — handy for converting one delimited format to another.

Other options #

OptionEffect
-z, --zero-terminatedTreat the NUL character as the line delimiter, so a "line" may itself contain newlines.

Exit status #

CodeMeaning
0Success.
1A mode was missing or given twice, an option was misused, or a file could not be read.

Edit this page