cut Command

The cut command extracts specific columns, fields, or character positions from text files or input streams. It's essential for processing structured data like CSV files and extracting specific information from text.

Syntax

cut [OPTION]... [FILE]...

Description

The cut command removes sections from each line of files. It can extract text by character position, by field (using delimiters), or by byte position. It's commonly used for processing CSV files, log files, and other structured text data.

Key features:

  • Extract specific character positions
  • Extract fields using delimiters
  • Process multiple files
  • Support for custom delimiters
  • Efficient text processing

Common Options

Option Description
-c LIST Extract characters at specified positions
-f LIST Extract specified fields
-d DELIM Use DELIM as field delimiter (default: tab)
-b LIST Extract specified bytes
-s Only output lines containing delimiter
--complement Extract everything except specified positions
--output-delimiter=STRING Use STRING as output delimiter

List Formats

Format Description Example
N Single position N -c5 (character 5)
N- From position N to end -c5- (from char 5 to end)
N-M From position N to M -c5-10 (chars 5 to 10)
-M From beginning to position M -c-5 (first 5 chars)
N,M Positions N and M -f1,3 (fields 1 and 3)

Examples

Extract specific characters

cut -c1-5 file.txt

Extracts characters 1 through 5 from each line

Extract first 10 characters

cut -c-10 file.txt

Extracts the first 10 characters from each line

Extract from character 5 to end

cut -c5- file.txt

Extracts from character 5 to the end of each line

Extract specific fields (tab-delimited)

cut -f1,3 file.txt

Extracts fields 1 and 3 from tab-delimited data

Extract from CSV file

cut -d',' -f2 data.csv

Extracts the second field from comma-separated data

Extract multiple fields from CSV

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

Extracts fields 1, 3, and 5 from CSV file

Extract with custom output delimiter

cut -d',' -f1,2,3 --output-delimiter=' | ' data.csv

Extracts fields and separates them with " | "

Extract from colon-separated file

cut -d':' -f1,6 /etc/passwd

Extracts username and home directory from passwd file

Only lines with delimiter

cut -d',' -f2 -s data.csv

Only processes lines that contain the comma delimiter

Extract everything except specified fields

cut -d',' -f2 --complement data.csv

Extracts all fields except field 2

Process multiple files

cut -d',' -f1 file1.csv file2.csv

Extracts first field from multiple CSV files

Extract from command output

ps aux | cut -c1-20

Extracts first 20 characters from ps command output

Common Use Cases

  • CSV Processing: Extract specific columns from CSV files
  • Log Analysis: Extract timestamps or specific fields from log files
  • System Information: Extract user names, IDs from system files
  • Data Cleaning: Remove unwanted columns from data files
  • Report Generation: Create summaries from structured data

See also