You have a CSV file, a line of /etc/passwd or a log, and you need a single column. cut reads each line, cuts it at a delimiter and prints only the fields you ask for. It computes nothing, reorders nothing and does not merge spaces. This simplicity explains most of its surprises.
The lesson Transform text with cut, tr and paste presents the main options of cut. This page adds the step-by-step splitting, the -s option, the errors cut does not report, and the point where you should switch to awk.
What you will learn
- Choose fields with
-dand-f, as a list, a range or up to the end of the line - Choose character positions with
-c, and know why accents cause trouble - Skip lines that have no delimiter with
-s - Invert the selection with
--complementand change the output delimiter - Spot the errors
cutdoes not report: ignored order, missing field, multiple spaces - Prepare spaces with
tr -sand pickawkwhencutis no longer enough
Prerequisites
You know how to open a Linux terminal, create a folder and move into it with cd. Redirections and pipes (|, <) are used in the examples. tr is presented in Transform text. To sort and count, the lesson Sort, count and deduplicate details sort and uniq.
Create the practice folder
All the examples run on the same files, in ~/cut-lab. comptes.csv contains a comment line with no comma and an empty field, two cases that cut handles in its own way. services.tsv is separated by tabs, espaces.txt aligns its columns with spaces, equipe.csv is used for counting, and accents.txt contains an accented character.
mkdir ~/cut-lab && cd ~/cut-labprintf '%s\n' 'Alice,web,Lyon' '# absents' 'Bob,,Nice' 'Chloe,db,Brest' > comptes.csvprintf 'serveur\tport\tservice\nlyon-01\t22\tssh\nnice-02\t443\thttps\n' > services.tsvprintf '%s\n' 'web-01 Lyon 80' 'db-02 Nice 5432' 'cache-3 Brest 6379' > espaces.txtprintf '%s\n' '# annuaire' 'Alice,admin,Lyon' 'Bob,dev,Nice' 'Chloe,dev,Brest' 'Diana,admin,Nice' 'Eve,ops,Lyon' 'Farid,dev,Lyon' > equipe.csvprintf 'Écran\n' > accents.txtThe outputs on this page come from GNU coreutils 9.4 (Ubuntu 24.04) under a UTF-8 locale; cut --version | head -1 prints your version. They are the same on Debian 13 (9.7), AlmaLinux 10 (9.5) and Ubuntu 26.04 (cut from uutils 0.8.0), with two exceptions: -c on accented text, and the wording of the error messages on Ubuntu 26.04. cut never modifies a file: it writes to standard output, so you risk nothing. The file contents use French words, which the commands keep: comptes is "accounts", equipe "team", espaces "spaces", annuaire "directory", absents "absentees", and serveur "server".
The cut command in detail
Without a file, cut reads standard input, which puts it naturally in the middle of a pipe. Without -f, -c or -b (bytes), it refuses to work. Read the line from left to right: each number points to the explanation of that piece.
Anatomy of the command
Syntaxcut -d delimiter -f fields [file...]
cut-d,-s-f1,3comptes.csv -
cutthe programThe command run in the shell. -
-d,the delimiteroptionalA single character. Without-d,cutcuts at tabs. -
-san optionoptional-sskips the lines with no delimiter. -
-f1,3the fieldsA list (1,3), a range (2-4), open-ended (2-,-3).-cchooses positions instead. -
comptes.csvthe fileoptionalOne or more files. Without a file,cutreads standard input.
OutputAlice,Lyon · Bob,Nice · Chloe,Brest
How cut splits a line, field by field
cut handles each line on its own: it cuts the line at every delimiter, numbers the pieces from 1 and keeps those in the list. The map below works on comptes.csv.
Before you turn the options on, predict what each line becomes for cut -d, -f1,3 comptes.csv, in particular # absents, which has no comma, and Bob,,Nice, whose field 2 is empty. Ask the same question with -s, with -f3,1 instead of -f1,3, then with both. Check on the map: look at the output list and the text of each step.
Mental model
How cut splits a line
read a linecut it at the delimiterkeep the requested fields and start again, line after line
Try Input · comptes.csv
The options
Fields found
$ cut -d, -f1,3 comptes.csv
- cut reads line 1 and cuts it at each comma: 3 fields.
- cut keeps fields 1 and 3 and prints them, separated by the same comma.
- cut reads line 2. It finds no comma: there is nothing to cut.
- With no delimiter, cut prints the whole line, with no message. A comment line ends up in the result.
- Field 2 is empty, and still counts: Nice remains field 3.
- cut keeps fields 1 and 3 and prints them, separated by the same comma.
- cut reads line 4 and cuts it at each comma: 3 fields.
- cut keeps fields 1 and 3 and prints them, separated by the same comma.
- No more lines. cut has not changed the file, and it returned the code 0.
What the animation shows
- One line at a time.
cutcuts at each delimiter, a single character. - An empty field counts. Two commas in a row surround an empty field.
- The order in `-f` is ignored:
-f3,1prints 1 then 3. - A line with no delimiter comes out whole, except with
-s.
Golden rules
Squeeze spaces before cutting
cut does not group spaces: two spaces make an empty field. Squeeze them first:
df -h | tr -s ' ' | cut -d' ' -f5Switch to awk to reorder
To change the order of columns, test or compute:
awk -F, '{ print $3, $1 }' comptes.csvEssential vocabulary
Choose · by fields or by position
-f2- Field 2
cut -d, -f2 comptes.csv→ 4 lines, one of them empty -f2-- From field 2 to the end
cut -d, -f2- comptes.csv→ 4 lines -c1-3- Characters 1 to 3
cut -c1-3 comptes.csv→ 3 characters per line --complement- Everything but field 2
cut -d, -f2 --complement comptes.csv→ 4 lines, without field 2
Adjust · delimiters and lines
-d,- The input delimiter
cut -d, -f1 comptes.csv→ field 1 of each line -s- Skip the lines with no delimiter
cut -d, -s -f1 comptes.csv→ 3 lines: Alice, Bob, Chloe --output-delimiter- The output delimiter
cut -d, -s -f1,3 --output-delimiter=' ' comptes.csv→ Alice Lyon, then Bob Nice, Chloe Brest -d' '- Spaces not grouped: empty field
echo 'a b' | cut -d' ' -f2→ an empty line
Choose fields: -d and -f
-d gives the delimiter, a single character, and -f the list of fields to keep. To extract the name and the city from comptes.csv:
cd ~/cut-labcut -d, -f1,3 comptes.csvAlice,Lyon# absentsBob,NiceChloe,BrestEach line keeps fields 1 and 3, separated by the same comma. The # absents line comes out whole: it contains no comma, so cut cannot split it and lets it through. The delimiter can be any single character: cut -d: -f1,7 /etc/passwd extracts the account and its shell.
Selection ranges
The -f list accepts single fields (1,3), ranges (2-3) and two open forms, 3- (from field 3 to the end) and -2 (from the start to field 2). The open forms save you from knowing the number of columns.
cut -d, -f-2 comptes.csvAlice,web# absentsBob,Chloe,dbLook at Bob's line: its field 2 is empty, but an empty field counts. -f-2 returns Bob, (Bob, then the empty field). Two commas in a row always surround a field. The same range grammar works for -c. A lone dash (-f-) is refused: cut: invalid range with no endpoint: -.
Forgetting the delimiter (tab by default)
Without -d, cut splits at tabs, not at commas. On a CSV it finds nothing to split and returns everything.
cut -f1 comptes.csvAlice,web,Lyon# absentsBob,,NiceChloe,db,BrestThe whole file is printed, with no message, and the exit code is 0. A file that comes out whole usually points to a wrong delimiter: add -d,. To find out which separator a file really contains, cat -A (section "Troubleshooting") shows the tabs.
TSV files (tab)
A TSV file separates its columns with tabs, the default delimiter: cut -f2 services.tsv prints the port column (the header, then 22 and 443), without -d.
Choose positions: -c
-c chooses character positions instead of fields, with no delimiter. It suits fixed-width files, where each column always starts at the same position. In espaces.txt, the city sits at positions 10 to 14.
cut -c10-14 espaces.txtLyonNiceBrestLyon and Nice come out with an extra space, because the range always takes five characters. The method stops working as soon as a value overflows its column and shifts what follows. -c accepts neither -d nor -f: cut refuses the command.
Multi-byte UTF-8 characters
An accented character takes two bytes in UTF-8. On Ubuntu 24.04, Ubuntu 26.04 and Debian 13, -c behaves like -b: it counts bytes, not characters.
cut -c1-5 accents.txtÉcraÉcran is five characters but six bytes (É takes two): -c1-5 cuts before the n. Setting LANG to a UTF-8 locale changes nothing. AlmaLinux 10 is the exception: under a UTF-8 locale, -c counts characters there and returns Écran. A script that must run everywhere therefore does not use -c on accented text.
Adjust the output: -s, --complement and the delimiter
Three options change what cut writes without changing the way it splits.
Skip lines without a delimiter
By default, a line with no delimiter comes out whole. -s drops it: cut -d, -s -f1 comptes.csv returns Alice, Bob and Chloe, without # absents. Bob's line stays, because it contains commas. A comment that contains a comma stays too: filter it out first with grep -v '^#'.
Invert the selection
--complement keeps everything except the listed fields: cut -d, -s -f2 --complement comptes.csv removes field 2 and returns Alice,Lyon, Bob,Nice and Chloe,Brest.
Change the output delimiter
--output-delimiter replaces the separator in the result: cut -d, -s -f1,3 --output-delimiter=';' comptes.csv returns Alice;Lyon, Bob;Nice and Chloe;Brest. cut interprets no escape sequence: --output-delimiter='\t' writes the two characters \ and t (Alice\tLyon). For a real tab, use the $'\t' syntax of bash or zsh.
The mistakes cut does not report
In the four cases below, cut returns code 0 and writes no message. Read the output, never the exit code.
Fields come out in file order
cut -d, -s -f3,1 comptes.csv returns Alice,Lyon, like -f1,3: cut always follows the order of the line, never that of the list. To reorder, switch to awk -F, '{ print $3 "," $1 }' comptes.csv, which returns Lyon,Alice.
Non-existent field
A field number that is too high causes no error: every line that contains the delimiter returns an empty line.
cut -d, -f9 comptes.csv# absentsThe three empty lines match the three lines with commas; # absents comes out whole. If an extraction returns only empty lines, count the fields with head -1 comptes.csv | tr ',' '\n' | wc -l.
Multiple spaces as delimiter
cut -d' ' treats every space as a separator: two spaces in a row surround an empty field. On espaces.txt, whose columns are aligned with several spaces, cut -d' ' -f2 espaces.txt returns three empty lines. First squeeze each run of spaces to a single one with tr -s ' ': the exercise below has you write it.
CSV with quoted fields
A CSV that puts quotes around fields containing a comma ("Alice, Bob",email) is split wrongly: cut knows nothing about quotes and splits at the inner comma. You need a CSV parser, for example the csv module of Python.
A column whose number changes
A colleague notices that cut -d' ' -f4 espaces.txt prints Lyon for web-01, and concludes that the city is field 4. Check on the other two servers, then write a command that prints the server and the city of all three.
Try field 4
Run the colleague's command and count the empty lines. Then run the same command with another field number, until you find the city of db-02.
Write the command that works for all three
Squeeze the runs of spaces before cut, then print the server and the city.
- cut -d
- tr -s
A hint 1 / 1
Count the spaces between the server name and the city on each line: every space is a separator.
I have tried: show the solution
cut -d' ' -f4 espaces.txtcut -d' ' -f5 espaces.txttr -s ' ' < espaces.txt | cut -d' ' -f1,2web-01 Lyondb-02 Nicecache-3 BrestThe colleague's command prints Lyon and two empty lines; with -f5, only Nice comes out. Three spaces separate web-01 from Lyon, so two empty fields: the city is field 4. Four spaces follow db-02: the city is field 5 there. No number fits all three lines, and cut returns code 0 on every attempt. With tr -s ' ', only one space is left between the columns: the city is field 2 everywhere. cut -c1-14 espaces.txt also gives the server and the city, as long as the columns stay aligned.
Chain cut with other commands
cut is mostly used in the middle of a pipe: grep chooses the lines, cut isolates a column, sort and uniq count. With grep, filter before extracting: grep ',dev,' equipe.csv | cut -d, -f1 returns the names with the dev role. To count the values of a column, add sort then uniq -c, as in Sort, count and deduplicate.
Count people per city
You want to know which city has the most people in equipe.csv. The file starts with a # annuaire line, which must not be counted as a city.
Extract, then count
Write the cut command that prints only the six cities, then add to the pipe the commands that sort, count and rank from the most frequent to the least frequent.
- cut -d
- sort
- uniq -c
A hint 1 / 1
Seven lines instead of six: reread the section on lines without a delimiter. uniq only counts neighbouring lines: it needs a sorted list.
I have tried: show the solution
cut -d, -s -f3 equipe.csv | sort | uniq -c | sort -rn 3 Lyon 2 Nice 1 BrestWithout -s, # annuaire would count as a fourth city. sort groups identical values so that uniq -c can count them, and sort -rn ranks the counts from largest to smallest. Lyon comes first with 3 people.
Which command should I use?
cut fits when the separator is a single, constant character and you keep the columns in their order. In the other cases, the table shows which tool to take.
| I want to... | Command |
|---|---|
| Take one column of a file with a single separator | cut -d, -f2 fichier.csv |
| Columns aligned with spaces | tr -s ' ' < fichier | cut -d' ' -f2 |
| Reorder or compute on columns | awk -F, '{ print $3 "," $1 }' fichier.csv |
| CSV with quotes | a CSV parser (csv module of Python) |
| Paste two files side by side | paste, see cut, tr and paste |
The lesson on awk details splitting by fields, conditions and calculations.
Troubleshooting
When cut does not return what you expect, the problem most often comes from the delimiter. Start by printing the first line with cat -A: it makes tabs (^I), line ends ($) and Windows carriage returns (^M) visible.
Diagnostic methods
cat -A services.tsv | head -2serveur^Iport^Iservice$lyon-01^I22^Issh$The ^I show that the separator is a tab: cut -f is enough, without -d. With a , instead, you would need -d,. Then test on a single line (head -1 fichier | cut -d, -f2) before running the full processing.
Frequent errors
The first two rows of the table are silent. The other messages concern the command line and return code 1. They are those of GNU coreutils: Ubuntu 26.04 words them differently, except the delimiter must be a single character.
| Symptom | Cause | Solution |
|---|---|---|
| The file comes out whole | Wrong delimiter | cat -A, then -d |
| Empty lines, code 0 | Non-existent field, or multiple spaces | Count the fields, or tr -s ' ' before cut |
cut: you must specify a list of bytes, characters, or fields | No -f, -c or -b | Add -f or -c |
cut: the delimiter must be a single character | -d,, or -d'::' | A single character, or switch to awk |
cut: an input delimiter may be specified only when operating on fields | -d with -c | Remove -d or use -f |
cut: fields are numbered from 1 | -f0 | Start at -f1 |
Clean up the practice folder
Delete the practice folder.
cd ~ && rm -r ~/cut-labKey points
cutsplits each line at a one-character delimiter (-d) and keeps the fields of-f, numbered from 1. Without-d, the delimiter is the tab: a CSV then comes out whole.- The field list accepts lists (
1,3), ranges (2-3) and open ranges (3-,-2).-f3,1comes out in the order 1 then 3:cutdoes not reorder. - A line with no delimiter comes out whole, except with
-s. An empty field counts, and a non-existent field returns an empty line, with no error and code 0. -cchooses positions, not fields, and refuses-d. On Debian and Ubuntu, it counts bytes: avoid it on accented text.cut -d' 'sees every space as a separator:tr -s ' 'squeezes them to one beforehand. To reorder, test a value or compute, switch toawk.
Knowledge check
Check that the essentials of this guide are acquired. The questions only cover what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: grep: search in text
A new full quiz with random questions
Work again only on the questions you missed