cut Command Guide for Extracting Columns
This guide helps you understand and use the cut command, the standard tool for slicing sections out of each line of text.
What is cut, and Why Use It?
The cut command extracts specific columns or characters from each line of a text stream. It works by fixed position — you tell it which character positions or which delimited fields to keep, and it prints only those parts. Its name literally means “to cut” pieces out of lines.
Why Learn cut?
- Column extraction: Pulling field 1 out of
passwdentries, or the second column ofpsoutput, iscut’s core job. - CSV-friendly: With
-dyou can split on any delimiter — commas, colons, tabs — and pick fields. - Fast and predictable: It’s simpler than
awkfor the common “just give me column N” case. - Works on raw bytes:
-ccuts by character position, whichawkcan’t do directly.
cutslices by fixed positions and delimiters. When you need computed fields, reach forawkinstead.
1. Cut by Delimiter (-d and -f)
The most common mode: pick a delimiter, then pick the field numbers.
cut -d: -f1 /etc/passwd
-d:— use the colon as the field separator.-f1— keep only field 1 (the username).
Output:
root
daemon
bin
Cut multiple fields — note they can be in any order, and you can list ranges:
cut -d: -f1,3,6 /etc/passwd
cut -d: -f2-4 /etc/passwd
-f1,3,6— fields 1, 3, and 6.-f2-4— fields 2 through 4 inclusive.-f3-— from field 3 to the end of the line.
A real-world example with comma-separated values:
cut -d, -f1,3 data.csv
If the line contains no delimiter,
cutprints the whole line — a common source of surprise when a file uses spaces instead of commas.
2. Cut by Character Position (-c)
When there’s no delimiter, cut by fixed character positions. This is ideal for fixed-width output like /etc/passwd in old System V layouts or reports with aligned columns.
cut -c1-8 notes.txt
- Keeps characters 1 through 8 of every line.
cut -c3 notes.txt
cut -c-4 notes.txt
cut -c5- notes.txt
-c3— just the third character.-c-4— from the start through character 4.-c5-— from character 5 to the end.
A classic fixed-width example — the first column of ls -l output is a permission string:
ls -l | cut -c1
- Every line starts with a permission character (
-,d,l, …), so this lists the file type column.
3. Cut by Bytes (-b)
-b works like -c but on bytes instead of characters. For plain ASCII text they’re identical; they differ on multi-byte UTF-8 characters.
cut -b1-3 data.txt
Prefer -c unless you specifically need byte-level control.
4. Complement: Invert the Selection (--complement)
Sometimes you want everything except the fields you named. --complement flips the selection.
cut -d: -f2- /etc/passwd --complement
- Prints every field except field 1 — i.e., everything after the username.
This is handy when one field is the exception rather than the rule:
cut -d, -f2 --complement data.csv
5. Cut Only Delimited Lines (-s)
By default, lines with no delimiter pass through unchanged (the whole line is printed). The -s flag suppresses those lines.
cut -d, -s -f2 data.csv
- Lines that contain a comma are cut as usual.
- Lines without a comma are dropped entirely.
This keeps malformed or header-like rows out of your output.
6. Combining cut with Other Commands
cut shines in pipelines, especially after commands that produce columnar output:
ps aux | cut -c1-80
Trims long process lines to a readable width.
ls -l | cut -d' ' -f5-
Extracts size-and-after from ls -l — though note that multiple spaces between fields make -d' ' unreliable here; awk handles that better.
cut -d: -f1,7 /etc/passwd | sort
Extract usernames and login shells, then sort them.
grep root /etc/passwd | cut -d: -f7
Show only the login shell of the root account.
Common Pitfalls
cutis delimiter-based, not whitespace-based. With-d' ', runs of multiple spaces create empty fields. For whitespace-aligned columns, useawkortr -s ' 'first.- Lines without the delimiter pass through whole. If your file mixes delimiters, add
-sor check the input first. - Field numbers start at 1. There is no
-f0. - Ranges use
-with no spaces.-f1-3is correct;-f 1 - 3is not. -ccounts characters,-bcounts bytes. With emoji or accented text these differ.
Related Pages
| Page | Relationship |
|---|---|
sort | cut extracts, sort orders — common partners in columnar pipelines. |
awk | The general-purpose cousin: computes fields instead of slicing them. |
tr | Translate/squeeze characters; often used to prep input for cut. |
uniq | Deduplicates the output of cut pipelines. |
wc | Counts what cut produced. |
cut Cheatsheet
| Flag | Meaning |
|---|---|
-d CHAR | Use CHAR as the field delimiter. |
-f LIST | Select fields: 1, 1,3, 2-4, 3-. |
-c LIST | Select character positions: 3, 1-8, 5-. |
-b LIST | Select byte positions (like -c, byte-based). |
--complement | Select everything except the listed fields/positions. |
-s | Suppress lines that contain no delimiter. |
Next: how to merge lines side by side — paste.