Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

cut: extract columns from a text file

By updated

Improve this page

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 -d and -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 --complement and change the output delimiter
  • Spot the errors cut does not report: ignored order, missing field, multiple spaces
  • Prepare spaces with tr -s and pick awk when cut is 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.

Terminal window
mkdir ~/cut-lab && cd ~/cut-lab
printf '%s\n' 'Alice,web,Lyon' '# absents' 'Bob,,Nice' 'Chloe,db,Brest' > comptes.csv
printf 'serveur\tport\tservice\nlyon-01\t22\tssh\nnice-02\t443\thttps\n' > services.tsv
printf '%s\n' 'web-01 Lyon 80' 'db-02 Nice 5432' 'cache-3 Brest 6379' > espaces.txt
printf '%s\n' '# annuaire' 'Alice,admin,Lyon' 'Bob,dev,Nice' 'Chloe,dev,Brest' 'Diana,admin,Nice' 'Eve,ops,Lyon' 'Farid,dev,Lyon' > equipe.csv
printf 'Écran\n' > accents.txt

The 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...]

  1. cut the programThe command run in the shell.
  2. -d, the delimiteroptionalA single character. Without -d, cut cuts at tabs.
  3. -s an optionoptional-s skips the lines with no delimiter.
  4. -f1,3 the fieldsA list (1,3), a range (2-4), open-ended (2-, -3). -c chooses positions instead.
  5. comptes.csv the fileoptionalOne or more files. Without a file, cut reads 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

$ cut -d, -f1,3 comptes.csv

  1. cut reads line 1 and cuts it at each comma: 3 fields.
  2. cut keeps fields 1 and 3 and prints them, separated by the same comma.
  3. cut reads line 2. It finds no comma: there is nothing to cut.
  4. With no delimiter, cut prints the whole line, with no message. A comment line ends up in the result.
  5. Field 2 is empty, and still counts: Nice remains field 3.
  6. cut keeps fields 1 and 3 and prints them, separated by the same comma.
  7. cut reads line 4 and cuts it at each comma: 3 fields.
  8. cut keeps fields 1 and 3 and prints them, separated by the same comma.
  9. No more lines. cut has not changed the file, and it returned the code 0.

What the animation shows

  1. One line at a time. cut cuts at each delimiter, a single character.
  2. An empty field counts. Two commas in a row surround an empty field.
  3. The order in `-f` is ignored: -f3,1 prints 1 then 3.
  4. 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' ' -f5

Switch to awk to reorder

To change the order of columns, test or compute:

awk -F, '{ print $3, $1 }' comptes.csv

Essential vocabulary

Choose · by fields or by position

-f2
Field 2cut -d, -f2 comptes.csv → 4 lines, one of them empty
-f2-
From field 2 to the endcut -d, -f2- comptes.csv → 4 lines
-c1-3
Characters 1 to 3cut -c1-3 comptes.csv → 3 characters per line
--complement
Everything but field 2cut -d, -f2 --complement comptes.csv → 4 lines, without field 2

Adjust · delimiters and lines

-d,
The input delimitercut -d, -f1 comptes.csv → field 1 of each line
-s
Skip the lines with no delimitercut -d, -s -f1 comptes.csv → 3 lines: Alice, Bob, Chloe
--output-delimiter
The output delimitercut -d, -s -f1,3 --output-delimiter=' ' comptes.csv → Alice Lyon, then Bob Nice, Chloe Brest
-d' '
Spaces not grouped: empty fieldecho '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:

Terminal window
cd ~/cut-lab
cut -d, -f1,3 comptes.csv
Output
Alice,Lyon
# absents
Bob,Nice
Chloe,Brest

Each 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.

Terminal window
cut -d, -f-2 comptes.csv
Output
Alice,web
# absents
Bob,
Chloe,db

Look 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.

Terminal window
cut -f1 comptes.csv
Output
Alice,web,Lyon
# absents
Bob,,Nice
Chloe,db,Brest

The 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.

Terminal window
cut -c10-14 espaces.txt
Output
Lyon
Nice
Brest

Lyon 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.

Terminal window
cut -c1-5 accents.txt
Output
É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.

Terminal window
cut -d, -f9 comptes.csv
Output
# absents

The 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 Exercise 1 of 2 · ~6 min · terminal

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.

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
Terminal window
cut -d' ' -f4 espaces.txt
cut -d' ' -f5 espaces.txt
tr -s ' ' < espaces.txt | cut -d' ' -f1,2
Output of the third command
web-01 Lyon
db-02 Nice
cache-3 Brest

The 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 Exercise 2 of 2 · ~8 min · terminal

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.

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
Terminal window
cut -d, -s -f3 equipe.csv | sort | uniq -c | sort -rn
Output
3 Lyon
2 Nice
1 Brest

Without -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 separatorcut -d, -f2 fichier.csv
Columns aligned with spacestr -s ' ' < fichier | cut -d' ' -f2
Reorder or compute on columnsawk -F, '{ print $3 "," $1 }' fichier.csv
CSV with quotesa CSV parser (csv module of Python)
Paste two files side by sidepaste, 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

Terminal window
cat -A services.tsv | head -2
Output
serveur^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.

SymptomCauseSolution
The file comes out wholeWrong delimitercat -A, then -d
Empty lines, code 0Non-existent field, or multiple spacesCount the fields, or tr -s ' ' before cut
cut: you must specify a list of bytes, characters, or fieldsNo -f, -c or -bAdd -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 -cRemove -d or use -f
cut: fields are numbered from 1-f0Start at -f1

Clean up the practice folder

Delete the practice folder.

Terminal window
cd ~ && rm -r ~/cut-lab

Key points

  • cut splits 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,1 comes out in the order 1 then 3: cut does 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.
  • -c chooses 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 to awk.

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

6 questions
6 min
70% to pass

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