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
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
Extracts characters 1 through 5 from each line
Extract first 10 characters
Extracts the first 10 characters from each line
Extract from character 5 to end
Extracts from character 5 to the end of each line
Extract specific fields (tab-delimited)
Extracts fields 1 and 3 from tab-delimited data
Extract from CSV file
Extracts the second field from comma-separated data
Extract multiple fields from CSV
Extracts fields 1, 3, and 5 from CSV file
Extract with custom output delimiter
Extracts fields and separates them with " | "
Extract from colon-separated file
Extracts username and home directory from passwd file
Only lines with delimiter
Only processes lines that contain the comma delimiter
Extract everything except specified fields
Extracts all fields except field 2
Process multiple files
Extracts first field from multiple CSV files
Extract from command output
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