Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 passwd entries, or the second column of ps output, is cut’s core job.
  • CSV-friendly: With -d you can split on any delimiter — commas, colons, tabs — and pick fields.
  • Fast and predictable: It’s simpler than awk for the common “just give me column N” case.
  • Works on raw bytes: -c cuts by character position, which awk can’t do directly.

cut slices by fixed positions and delimiters. When you need computed fields, reach for awk instead.


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, cut prints 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

  • cut is delimiter-based, not whitespace-based. With -d' ', runs of multiple spaces create empty fields. For whitespace-aligned columns, use awk or tr -s ' ' first.
  • Lines without the delimiter pass through whole. If your file mixes delimiters, add -s or check the input first.
  • Field numbers start at 1. There is no -f0.
  • Ranges use - with no spaces. -f1-3 is correct; -f 1 - 3 is not.
  • -c counts characters, -b counts bytes. With emoji or accented text these differ.

PageRelationship
sortcut extracts, sort orders — common partners in columnar pipelines.
awkThe general-purpose cousin: computes fields instead of slicing them.
trTranslate/squeeze characters; often used to prep input for cut.
uniqDeduplicates the output of cut pipelines.
wcCounts what cut produced.

cut Cheatsheet

FlagMeaning
-d CHARUse CHAR as the field delimiter.
-f LISTSelect fields: 1, 1,3, 2-4, 3-.
-c LISTSelect character positions: 3, 1-8, 5-.
-b LISTSelect byte positions (like -c, byte-based).
--complementSelect everything except the listed fields/positions.
-sSuppress lines that contain no delimiter.

Next: how to merge lines side by side — paste.