/flags/coreutils/cut-d-f-delimiter-fields
cut -d -f: extract fields from lines by a delimiter
- 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
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 -1Output
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
| LIST | Selects |
|---|---|
| N | Field N (counted from 1) |
| N,M | Fields N and M, always in input order |
| N-M | Fields N through M |
| N- | Field N to the end of the line |
| -M | Fields 1 through M |
Options that change the result
| Option | Effect |
|---|---|
| -s, --only-delimited | Skip lines that contain no delimiter (otherwise they are printed whole) |
| --complement | Print every field except the selected ones (GNU) |
| --output-delimiter=STR | Join the selected fields with STR instead of DELIM (GNU) |
| -c LIST | Select characters instead of fields |
| -b LIST | Select bytes |
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.txtOutput
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 bsplit on a space has an empty field 2. Usetr -s ' 'first or switch to awk, described in awk -F field separator. - Quoted CSV. cut knows nothing about quotes:
"Doe, Jane",devhas three fields for cut. Use a CSV-aware tool for real CSV exports. - One character only. A longer
-dvalue is rejected:
echo 'a::b' | cut -d'::' -f2Output · exit 1
cut: the delimiter must be a single character Try 'cut --help' for more information.
- No field list. Running
cutwithout-b,-cor-ffails and asks you to specify a list of bytes, characters or fields. --complementand--output-delimiterare 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.