Text-Fu · Lesson 6 of 16
cut
0%
0 / 5 checkpoints answered
Click inside the terminal, type the command, press Enter.
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.
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.
CheckWhich command prints the first character from every line of names.txt?
press 1/2/3 to answer · → next lesson
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-.
CheckWhich command prints the second tab-delimited field from every line of team.tsv?
press 1/2/3 to answer · → next lesson
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.
CheckWhich command prints the second semicolon-delimited field from team.txt?
press 1/2/3 to answer · → next lesson
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.
CheckWhat does -s do with cut -d ':' -f 1?
press 1/2/3 to answer · → next lesson
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
CheckIn generate-data | cut -d ':' -f 1, where does cut read its input?
press 1/2/3 to answer · → next lesson
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.
Summary
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.
Practice
In the practice terminal below: 1. Make a tab-separated file: printf 'name\trole\nalice\tadmin\nbob\tviewer\n' > /tmp/team.tsv 2. Pick the second field: cut -f 2 /tmp/team.tsv It should print role, admin, viewer - the second tab-separated field of each line.
cut -f selects fields; the default delimiter is a tab.
Lesson complete
You finished cut. Nice work — keep the momentum going.