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:
| Option | A column is… |
|---|---|
-b, --bytes | a single byte. |
-c, --characters | a single character. |
-f, --fields | a 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.
| Sequence | Selects |
|---|---|
3 | column 3 only. |
2,5,9 | columns 2, 5, and 9. |
5-7 | columns 5 through 7. |
3- | column 3 to the end of the line. |
-4 | the start of the line through column 4. |
1,4-6,9 | any mixture of the above. |
| Option | Effect |
|---|---|
--complement | Invert the selection — keep every column except those named. |
Field mode: the delimiter #
In field mode, cut needs to know what separates the fields.
| Option | Effect |
|---|---|
-d, --delimiter=CHAR | The character that separates fields. The default is a tab. |
-w | Separate fields on runs of whitespace (spaces and tabs) instead of a single character. Cannot be combined with -d. |
-s, --only-delimited | Print only lines that actually contain the delimiter; drop lines that have no fields to cut. |
--output-delimiter=STRING | Put STRING between the kept fields in the output, instead of repeating the input delimiter — handy for converting one delimited format to another. |
Other options #
| Option | Effect |
|---|---|
-z, --zero-terminated | Treat the NUL character as the line delimiter, so a "line" may itself contain newlines. |
Exit status #
| Code | Meaning |
|---|---|
0 | Success. |
1 | A mode was missing or given twice, an option was misused, or a file could not be read. |