The cut command selects specified character positions or fields from each input line. It works best with consistently structured text whose delimiters and field positions are known.
Text-Fu · Lesson 6
cut
Learn how to select character positions or delimited fields from each line with cut.
Create a small tab-separated file for the examples. printf interprets \t as a literal tab and \n as a newline:
$ printf 'name\trole\nalice\tadmin\nbob\tviewer\n' > team.tsv
Selecting Character Positions
Use -c LIST to select positions from each line. Positions start at 1:
$ cut -c 1 team.tsv
n
a
b
The list can contain individual positions and ranges:
$ cut -c 1-4 team.tsv
name
alic
bob
$ cut -c 1,3 team.tsv
nm
ai
bb
Spaces, tabs, and punctuation occupy positions too. cut processes each line independently.
Which command prints the first character from every line of names.txt?
Selecting Tab-Delimited Fields
Use -f LIST to select fields. The default delimiter is a tab:
$ cut -f 2 team.tsv
role
admin
viewer
As with character selection, a list can include values such as 1, 1,3, 2-4, -3, or 2-.
Which command prints the second tab-delimited field from every line of team.tsv?
Choosing a Custom Delimiter
Use -d CHARACTER with -f when fields use a delimiter other than a tab. This example creates semicolon-separated data:
$ printf 'alice;admin\nbob;viewer\n' > team.txt
$ cut -d ';' -f 1 team.txt
alice
bob
The delimiter for this form is one character. Quote ; because an unquoted semicolon has control meaning in the shell.
Which command prints the second semicolon-delimited field from team.txt?
Handling Lines without the Delimiter
In field mode, cut normally prints a line unchanged when it contains no delimiter. Add -s to suppress those lines:
$ printf 'alice;admin\nheader\nbob;viewer\n' | cut -s -d ';' -f 2
admin
viewer
This does not validate a general CSV file. CSV can contain quoted delimiters, embedded newlines, and escaping rules that a single-character split does not understand; use a CSV-aware tool for such data.
What does -s do with cut -d ':' -f 1?
Reading from stdin
When no file is named, or when - is used as an input operand, cut reads stdin. That makes it a natural pipeline stage:
$ printf 'red:1\nblue:2\n' | cut -d ':' -f 1
red
blue
In generate-data | cut -d ':' -f 1, where does cut read its input?
To practice positional and field selection, try these hands-on labs:
- Linux cut Command: Text Cutting - This lab provides a direct, hands-on introduction to the
cutcommand, allowing you to practice extracting specific columns or fields from text files, just as discussed in the lesson. - Sequence Control and Pipeline - Enhance your command-line efficiency by learning to control command execution sequences, utilize pipelines, and leverage powerful text processing tools like
cut,grep,wc,sort, anduniq.
Lesson complete
You finished cut
You can now select predictable positions from line-oriented text with cut.
Select individual character positions or ranges.
Extract tab-delimited fields with
-f.Supply a one-character delimiter with
-d.Suppress undelimited lines when appropriate.
Read structured text from files or stdin.
Keep your learning progress
Create a free account to save this lesson and continue learning on any device.
Create a free account