Terminal GuideTerminal Guide

cut Command Guide

The cut command extracts portions of each line from files. Learn how to select columns, fields, and character ranges.

5 min readLast updated: 2024
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Fields

cut -f 1 fileFirst field (tab sep)
cut -f 1,3 fileFields 1 and 3
cut -f 1-3 fileFields 1 to 3

Delimiter

-d ":"Use : as delimiter
-d ","Use , (CSV)
-d " "Use space

Characters

-c 1-5Chars 1 to 5
-c 1,5,10Specific chars
-c 5-From char 5 to end

Common

cut -d: -f1 /etc/passwdGet usernames
cut -d, -f2 file.csvCSV column 2
cut --complement -f1All except field 1

Downloadable Image Preview

Failed to generate preview

Basic Usage

cut extracts specific portions from each line of input.

bash
# Cut by field (default delimiter is tab)
cut -f1 file.txt

# Cut by character position
cut -c1-10 file.txt

Common Options

cut Options

-f LISTSelect fields (columns)
-d CHARUse CHAR as field delimiter
-c LISTSelect characters
-b LISTSelect bytes
--complementSelect everything except specified
-sOnly print lines with delimiter

Selecting Fields

bash
# Single field (tab-delimited)
cut -f2 file.txt

# Multiple fields
cut -f1,3 file.txt

# Range of fields
cut -f2-4 file.txt

# Field 2 to end
cut -f2- file.txt

Custom Delimiter

bash
# CSV (comma-separated)
cut -d',' -f1,2 data.csv

# Colon-separated (like /etc/passwd)
cut -d':' -f1 /etc/passwd

# Space-separated
cut -d' ' -f2 file.txt
Tip
For complex CSV files with quoted fields, consider using awk or specialized tools like csvcut.

Selecting Characters

bash
# First 5 characters
cut -c1-5 file.txt

# Character 5 to end
cut -c5- file.txt

# Specific characters
cut -c1,3,5 file.txt

# First 3 and last 3 characters
cut -c1-3,-3 file.txt

Complement Selection

bash
# All fields EXCEPT field 2
cut -f2 --complement data.csv

# All characters EXCEPT first 5
cut -c1-5 --complement file.txt

Practical Examples

Extract usernames from /etc/passwd

bash
cut -d':' -f1 /etc/passwd

Get specific columns from CSV

bash
cut -d',' -f1,3,5 data.csv

Extract IP addresses from log

bash
cut -d' ' -f1 access.log

Get file extensions

bash
ls | cut -d'.' -f2

Extract date from timestamp

bash
echo "2024-01-15 10:30:00" | cut -d' ' -f1

Remove first column

bash
cut -f1 --complement data.txt

Extract domain from URLs

bash
echo "https://example.com/path" | cut -d'/' -f3

Process PATH variable

bash
echo $PATH | cut -d':' -f1-3

cut vs awk

awk is more powerful for complex field extraction:

bash
# cut: simple, fast, single delimiter
cut -d',' -f2 data.csv

# awk: handles multiple delimiters, calculations
awk -F',' '{print $2}' data.csv

# awk: can handle variable whitespace
awk '{print $2}' file.txt
Info
cut treats consecutive delimiters as separate fields. Use awk if your data has variable whitespace between fields.

Output Delimiter

bash
# Change output delimiter
cut -d':' -f1,3 --output-delimiter=',' /etc/passwd

Summary

cut is efficient for simple field extraction. Key takeaways:

  • Use cut -f N for field extraction
  • Use cut -d to set delimiter
  • Use cut -c for character positions
  • Use --complement to exclude selections
  • Consider awk for complex cases

Related Articles