Skip to main content

Syntax

cut -f FIELDS -d DELIMITER [FILE...]
cut -c CHARS [FILE...]

Description

The cut command extracts specific fields or character positions from each line of input. It’s essential for working with structured text like CSV files, tab-separated data, or fixed-width columns. Nash’s cut supports both field-based and character-based extraction with flexible range specifications.

Options

-f FIELDS
string
required
Select only these fields from each line. Field numbering starts at 1.Formats:
  • 1 - Single field
  • 1,3,5 - Multiple specific fields
  • 1-3 - Range of fields (1 through 3)
  • 2- - From field 2 to end (up to position 1000)
-d DELIMITER
string
default:"\\t"
Use specified delimiter to separate fields. Defaults to tab character.Only the first character of the delimiter string is used.
-c CHARS
string
required
Select only these character positions from each line. Position numbering starts at 1.Uses same format as -f: 1, 1,3,5, 1-3, 2-
You must specify either -f (fields) or -c (characters), but not both.

Examples

Extract single field

echo -e "name\tage\tcity" | cut -f2
age

Extract multiple fields

echo -e "Alice\t30\tNY\tUSA" | cut -f1,3
Alice	NY

Field range

echo -e "one\ttwo\tthree\tfour\tfive" | cut -f2-4
two	three	four

Custom delimiter (CSV)

echo "apple,banana,cherry,date" | cut -d, -f2,4
banana,date

Extract characters

echo "Hello World" | cut -c1-5
Hello

Specific character positions

echo "abcdefgh" | cut -c1,3,5,7
aceg

From position to end

echo "Hello World" | cut -c7-
World

Pipeline Examples

Extract column from CSV

cat data.csv | cut -d, -f2
Extracts the second column from a CSV file.

Process multiple files

cut -d: -f1 users.txt groups.txt
Extracts first field from both files.

Combine with grep

grep "active" users.csv | cut -d, -f1,3
Find active users and extract their name and email.

Pipeline transformation

cat data.tsv | cut -f2-4 | sort | uniq
Extract fields 2-4, sort them, and remove duplicates.

Extract and count

cut -d, -f3 sales.csv | sort | uniq -c
Extract third column, sort values, and count occurrences.

Practical Use Cases

Parse /etc/passwd format

echo "user:x:1000:1000:User Name:/home/user:/bin/bash" | cut -d: -f1,6
user:/home/user
Extracts username and home directory.

Extract email domains

cat emails.txt | cut -d@ -f2
Gets everything after the @ symbol.

Process log files

cat access.log | cut -d' ' -f1,4,9
Extracts IP address, timestamp, and status code (space-delimited).

CSV to specific columns

cat inventory.csv | cut -d, -f1,4,5 > simplified.csv
Creates a new CSV with only columns 1, 4, and 5.

Extract initials

echo "John Smith" | cut -c1 && echo "John Smith" | cut -d' ' -f2 | cut -c1
Extracts first character of first and last name.

Fixed-width data

echo "20240101USANYC1000" | cut -c1-8
20240101
Extracts date from fixed-position record.

Create new format

cat names.csv | cut -d, -f2,1 --output-delimiter=" "
Note: Nash’s cut maintains the original delimiter in output.

Working with Different Formats

Tab-separated (TSV)

echo -e "col1\tcol2\tcol3" | cut -f1,3
Default delimiter is tab - no -d needed.

Pipe-delimited

echo "a|b|c|d" | cut -d'|' -f2,4
b|d

Space-delimited

echo "one two three four" | cut -d' ' -f2-3
two three

Colon-delimited

echo "key:value:type:count" | cut -d: -f2,4
value:count

Advanced Examples

Multi-stage extraction

echo "[email protected]" | cut -d@ -f1
user

Extract and filter

cat data.csv | cut -d, -f3 | grep -v "^$" | sort -u
Extract column 3, remove empty lines, show unique values.

Join columns from multiple sources

echo "$(cut -d, -f1 names.csv) $(cut -d, -f1 scores.csv)"
Combines first columns from two files.

Range to end

echo "a,b,c,d,e,f" | cut -d, -f3-
c,d,e,f

Tips

Field numbering starts at 1: The first field is 1, not 0. This matches standard Unix cut behavior.

Delimiter output

Cut preserves the original delimiter in output:
echo "a,b,c" | cut -d, -f1,3  # Output: a,c

Open-ended ranges

Use N- to select from field N to the end:
cut -d, -f3- data.csv  # Fields 3, 4, 5, ... to end

Character vs field modes

  • Field mode (-f): Splits by delimiter, extracts fields
  • Character mode (-c): Counts characters, extracts positions
echo "a,b,c" | cut -f2 -d,    # Field: b
echo "a,b,c" | cut -c3         # Character: b
  • grep - Filter lines by pattern
  • sed - Stream editor
  • sort - Sort lines
  • uniq - Filter duplicates

Build docs developers (and LLMs) love