← Full reference

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

OptionWhat it does
-f listselect fields
-d 'c'field delimiter — a single character; tab by default
-sskip lines that have no delimiter at all (by default they're printed whole)
-c listselect characters by position (in GNU cut it's effectively bytes, like -b)
-b listselect bytes by position
--complementprint everything except the selection
--output-delimiter='STR'what to join the selected fields or ranges with on output (default — the input delimiter)
-zlines are separated by a NUL byte, not \n

List notation

NotationMeaning
3the third field (position)
1,3,5several, comma-separated
2-4from the 2nd to the 4th inclusive
3-from the 3rd to the end of the line
-2from 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 b field 2 is empty and b is the third. For output like ls -l or ps, squeeze spaces with tr -s ' ' or use awk, which splits on any run of spaces.
  • Multi-character delimiters aren't supported. cut -d'::' fails with the delimiter must be a single character.
  • Field order doesn't change. -f3,1 prints the fields in order 1, 3. To reorder columns you need awk.
  • Lines without the delimiter are printed whole. echo nodelim | cut -d: -f2 prints nodelim. Add -s to skip them.
  • -c counts bytes. In GNU cut -c works 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 by cut: the comma inside the quotes becomes a delimiter too.
  • A space at the start of a line is also a field boundary. In a b with -d' ' the first field is empty.

See also

awk · tr · sort · datamash