Skip to content

/flags/coreutils/cut-d-f-delimiter-fields

cut -d -f: extract fields from lines by a delimiter

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · ls (GNU coreutils) 8.32updated
tool
GNU coreutils
command
cut
flag
-d DELIM, -f LIST
default
-d is TAB; -f has no default (one of -b, -c or -f is required)

cut -d, -f2 file.csv prints the second comma-separated field of every line. -f LIST selects fields and -d DELIM sets the single character that separates them; without -d the delimiter is a TAB, which is why cut -f2 on space-separated text prints whole lines. LIST accepts numbers, ranges and open ranges: -f1,3, -f2-4, -f3-.

Examples

bash
printf 'name,role,team\nana,dev,core\nbo,ops,infra\n' > users.csv
cut -d, -f1 users.csv
echo '--- fields 1 and 3'
cut -d, -f1,3 users.csv
echo '--- from field 2 on'
cut -d, -f2- users.csv
echo '--- default delimiter is TAB'
printf 'a\tb c\td\n' | cut -f2
printf 'a\tb c\td\n' | cut -d' ' -f2 | od -c | head -1

Output

name
ana
bo
--- fields 1 and 3
name,team
ana,core
bo,infra
--- from field 2 on
role,team
dev,core
ops,infra
--- default delimiter is TAB
b c
0000000   c  \t   d  \n

The last two commands show the default: with TAB as the delimiter, field 2 is b c; with a space it is c, a TAB and d, because only the space splits.

Field list syntax

LISTSelects
NField N (counted from 1)
N,MFields N and M, always in input order
N-MFields N through M
N-Field N to the end of the line
-MFields 1 through M

Options that change the result

OptionEffect
-s, --only-delimitedSkip lines that contain no delimiter (otherwise they are printed whole)
--complementPrint every field except the selected ones (GNU)
--output-delimiter=STRJoin the selected fields with STR instead of DELIM (GNU)
-c LISTSelect characters instead of fields
-b LISTSelect bytes
bash
printf 'a,b,c\nno delimiter here\n' > mixed.txt
cut -d, -f2 mixed.txt
echo '--- with -s'
cut -s -d, -f2 mixed.txt
echo '--- complement and output delimiter'
cut -d, -f2 --complement --output-delimiter=';' mixed.txt

Output

b
no delimiter here
--- with -s
b
--- complement and output delimiter
a;c
no delimiter here

A line with no delimiter is printed unchanged unless -s is given, which surprises people parsing files with comments or headings mixed into the data.

Pitfalls and errors

  • Repeated delimiters. Each delimiter counts, so a b split on a space has an empty field 2. Use tr -s ' ' first or switch to awk, described in awk -F field separator.
  • Quoted CSV. cut knows nothing about quotes: "Doe, Jane",dev has three fields for cut. Use a CSV-aware tool for real CSV exports.
  • One character only. A longer -d value is rejected:
bash
echo 'a::b' | cut -d'::' -f2

Output · exit 1

cut: the delimiter must be a single character
Try 'cut --help' for more information.
  • No field list. Running cut without -b, -c or -f fails and asks you to specify a list of bytes, characters or fields.
  • --complement and --output-delimiter are GNU extensions; macOS cut does not have them, so portable scripts use awk for those cases.

FAQ

Why does cut -d' ' not work on text aligned with several spaces?

cut treats every single delimiter as a field boundary, so two spaces in a row create an empty field. Squeeze the spaces first with tr -s ' ', or use awk '{print $2}', which splits on runs of blanks.

Can cut use a delimiter longer than one character?

No. -d accepts exactly one character; GNU cut prints 'the delimiter must be a single character'. Use awk -F 'string' or sed for multi-character separators.

How do I reorder columns with cut?

You cannot. cut -f 3,1 prints fields 1 and 3 in their original order. Use awk -F, -v OFS=, '{print $3, $1}' to change the order.

Sources