The Linux cut Command: Extracting Columns Precisely
The Linux cut Command: Extracting Columns Precisely
I had a 10GB CSV file and needed just column 5. Loading into Excel wasn't an option. "Use cut," my colleague said. Three minutes later, I had my data. Let me share what I learned.
First cut Commands
Extract by character
cut -c 1-10 file.txt
Characters 1-10.
Extract by field
cut -f 1 file.txt
First field (tab-separated).
Delimiter
cut -d',' -f 1 file.csv
First field, comma-separated.
cut Commands That Work
Multiple fields
cut -d',' -f 1,3,5 file.csv
Fields 1, 3, 5.
Range
cut -d',' -f 1-3 file.csv
Fields 1 through 3.
Suppress no delimiter
cut -s -d',' -f 1 file.csv
Skip lines without delimiter.
Output delimiter
cut -d',' -f 1,2 --output-delimiter='|' file.csv
Replace output delimiter.
The cut Command Builder
Building cut commands—the cut Command Builder:
- Field selector
- Delimiter options
- Output separator
Quick Reference
| Flag | What It Does |
|---|---|
-c |
Character |
-f |
Field |
-d |
Delimiter |
-s |
Suppress |
--output-delimiter |
Output sep |
Conclusion: cut Is Column Extractor
cut is perfect for CSV, TSV data extraction.
The cut Command Builder makes selection visual.
