cut — cut fields out of lines
The cut command takes fields out of every line by a delimiter (-d and -f) or characters by position (-c).
Cheat sheet
7 commands Printable version-
cut -d: -f1 /etc/passwdfirst field, ":" delimiter -
cut -d: -f1,7 /etc/passwdfields 1 and 7 -
cut -d, -f2- data.csvfrom field 2 to the end -
cut -d' ' -f1 access.logthe first word of each line -
cut -c1-8 file.txtpositions 1 through 8 -
cut -f2 table.tsvfield 2, TAB delimiter -
cut -d: -f1,6 --output-delimiter=' ' /etc/passwd
Overview
cut splits each line into fields at a single delimiter character and prints the
fields listed in -f. -d sets the delimiter; without it, the delimiter is a tab.
Fields are counted from one:
cut joins the selected fields with the same delimiter. Numbers can be a list
(1,3) or a range (2-4, 3- up to the end of the line). Instead of fields you can
cut by position: -c for characters, -b for bytes. On Ubuntu -c counts bytes too:
no difference for Latin text, but Cyrillic letters get cut in half.
cut knows only a single delimiter. Two spaces in a row mean an empty field between
them. Text aligned with spaces (output of ps, df, uniq -c) is split with
awk '{print $2}' — see sed and awk.
Options
7-f, -c and -b are three ways to pick part of a line; a single call needs exactly
one of them. The full list is in man cut.
| Option | What it does | Example |
|---|---|---|
-d --delimiter C |
Field delimiter — a single character; tab by default. | cut -d: -f1 /etc/passwd |
-f --fields LIST |
Which fields to print — 1,3 or 2-4 or 3-. | cut -d, -f2- data.csv |
-c --characters LIST |
Cut by position; on Ubuntu it counts bytes, like -b. | cut -c1-8 file.txt |
-s --only-delimited |
Skip lines that contain no delimiter. | cut -s -d= -f2 settings.conf |
--complement |
Print everything except the selected fields. | cut -d: --complement -f2 /etc/passwd |
--output-delimiter STR |
String to join output fields with instead of the input delimiter. | cut -d: -f1,7 --output-delimiter=' ' /etc/passwd |
-b --bytes LIST |
Cut by byte positions. | cut -b1-4 data.bin |
Nothing found. Try a shorter query.
Recipes
5Addresses from a web server log
A column from a CSV file
If CSV values contain commas inside quotes, cut will not understand them: you need
a tool that knows the format.
Everything except one field
Skip lines without the delimiter
Without -s, a line with no delimiter is printed whole.
Troubleshooting
7Empty output with a space delimiter
There is more than one space between words, so the field you asked for is empty.
Squeeze the spaces with tr -s ' ' before cut, or take the field with
awk '{print $2}'.
cut prints the whole line
The line has no -d delimiter, and cut prints it unchanged. Check the delimiter
(: or ,, tab or space), or add -s to skip such lines.
cut: you must specify a list of bytes, characters, or fields
You did not say what to cut. Add -f with field numbers or -c with positions.
cut: the delimiter must be a single character
-d got more than one character. cut takes a single one; for a multi-character
delimiter use awk -F.
cut: fields are numbered from 1
Fields are numbered from one: the first field is -f1, not -f0.
cut: only one type of list may be specified
The command has both -f and -c. Keep one of them.
Cyrillic letters are cut in half
On Ubuntu cut -c counts bytes, like -b, and a Cyrillic letter takes two bytes in
UTF-8. For non-Latin text, cut by fields (-d, -f) or use awk.
See also
- sort — order the column you cut out.
- uniq — count the repeats in a column.
- wc — how many lines you got.
- sed-awk — awk splits on any run of spaces and can count.
- redirection — how to build a pipeline.
- text-tools — tr, paste, column and other pipeline links.
Found an inaccuracy? Report an error in the article