cut
Cut columns by a delimiter or characters by position out of every line
cut prints only the selected parts of each line: fields separated by a single character, or a range of positions. It's the simplest way to take a column from a file with a clear delimiter — /etc/passwd, CSV without quotes, TSV.
Syntax
cut -d 'delimiter' -f list [file…]
cut -c list [file…]
cut -b list [file…]
Exactly one of -f, -c, -b is required. Without files (or with the file -) stdin is read.
Options
| Option | What it does |
|---|---|
-f list | select fields |
-d 'c' | field delimiter — a single character; tab by default |
-s | skip lines that have no delimiter at all (by default they're printed whole) |
-c list | select characters by position (in GNU cut it's effectively bytes, like -b) |
-b list | select bytes by position |
--complement | print everything except the selection |
--output-delimiter='STR' | what to join the selected fields or ranges with on output (default — the input delimiter) |
-z | lines are separated by a NUL byte, not \n |
List notation
| Notation | Meaning |
|---|---|
3 | the third field (position) |
1,3,5 | several, comma-separated |
2-4 | from the 2nd to the 4th inclusive |
3- | from the 3rd to the end of the line |
-2 | from the 1st to the 2nd |
1,4- | can be combined |
Numbering starts at 1. The selection is printed in the order it appears in the line, and only once, no matter how the list is written.
Examples
# user name and shell from /etc/passwd
cut -d: -f1,7 /etc/passwd
# all fields from the second one on
echo 'a:b:c:d' | cut -d: -f2-
# everything except the second field
echo 'a:b:c:d' | cut -d: --complement -f2
# change the delimiter on output
echo 'a:b:c' | cut -d: -f1,3 --output-delimiter=' '
# the first 3 characters of each line; everything from the 5th character
echo 'hello world' | cut -c1-3
echo 'hello world' | cut -c5-
# TSV: the default delimiter is a tab
printf 'id\tname\n1\talice\n' | cut -f2
# CSV without quotes inside values
printf 'x,10\ny,20\n' | cut -d, -f2
# only lines that contain the delimiter
printf 'a:b\nno-delimiter\n' | cut -s -d: -f2
# space-aligned output: squeeze the spaces first
printf 'a 1\nbb 2\n' | tr -s ' ' | cut -d' ' -f2
Pitfalls
- The delimiter is exactly one character. Several spaces in a row produce empty fields: in
a bfield 2 is empty andbis the third. For output likels -lorps, squeeze spaces withtr -s ' 'or use awk, which splits on any run of spaces. - Multi-character delimiters aren't supported.
cut -d'::'fails withthe delimiter must be a single character. - Field order doesn't change.
-f3,1prints the fields in order 1, 3. To reorder columns you need awk. - Lines without the delimiter are printed whole.
echo nodelim | cut -d: -f2printsnodelim. Add-sto skip them. -ccounts bytes. In GNU cut-cworks like-b, so an accented letter such asé(2 bytes in UTF-8) gets cut in half.- CSV with quotes (
"Smith, J.",42) isn't understood bycut: the comma inside the quotes becomes a delimiter too. - A space at the start of a line is also a field boundary. In
a bwith-d' 'the first field is empty.