cut
Extract columns or fields from each line of input
By CMD Script Team · 4 min read · Last updated
cut OPTION... [FILE...]Options
| Flag | Description |
|---|---|
-d | Set the field delimiter (default is TAB) |
-f | Select specific fields by number or range, e.g. -f1,3 or -f2-4 |
-c | Select specific character positions by number or range, e.g. -c1-10 |
-s | Suppress lines that don't contain the delimiter |
--complement | Invert the selection, printing everything except the specified fields |
--output-delimiter | Use a different delimiter for output than the one used for input |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS (BSD cut, no --complement or --output-delimiter)
What it does
cut extracts sections from each line of input and writes them to standard output. It
operates in one of two modes: by delimited field number (-f, using -d to set the
separator) or by raw character position (-c). It's a lightweight tool for pulling a
column out of structured text like /etc/passwd, CSV exports, or fixed-width command
output, without needing the full power of awk.
Beginner examples
cut -d':' -f1 /etc/passwd— print the first colon-delimited field (usernames)cut -d',' -f2 data.csv— print the second comma-delimited fieldcut -c1-5 file.txt— print the first 5 characters of each linecut -f1,3 data.tsv— print the first and third tab-delimited fields (default delimiter)
cut -d':' -f1,3 /etc/passwd
Advanced examples
- Extract a range of fields:
cut -d',' -f2-4 sales.csv - Print everything except a field using an open-ended range:
cut -d':' -f2- /etc/passwd - Combine with
sortto build a unique list of usernames:cut -d':' -f1 /etc/passwd | sort - Use a different output delimiter than the input one (GNU cut):
cut -d',' -f1,2 --output-delimiter='|' data.csv - Pull a fixed-width column from
psoutput:ps aux | cut -c1-10
awk -F',' '{print NF}' data.csv | sort -u # sanity-check field count before cutting
Common mistakes
- Assuming
cuthandles quoted CSV fields correctly — it splits naively on the delimiter character, so a comma inside a quoted field will throw off the field count. - Forgetting
-dand getting no useful output because the default delimiter is TAB, not comma or space. - Using multiple spaces as a delimiter with
cut -d' 'and getting empty fields, sincecutdoesn't collapse repeated delimiters the wayawkdoes by default. - Expecting
cut -f3,1to reorder output —cutalways prints fields in original left-to-right order regardless of the order listed.
Tips
- For space-separated data with inconsistent spacing (like
ps aux), preferawk '{print $2}'overcut, sinceawkcollapses repeated whitespace automatically. - Use
cut -swhen processing mixed input so lines without the delimiter are dropped instead of printed as-is. - Check field counts first with
awk -F',' '{print NF}' file | sort -ubefore trustingcut -foutput on messy CSV data.
Best practices
- Reach for
cutonly on genuinely simple, single-character-delimited data; switch toawkonce you need quoting, multiple delimiters, or field reordering. - Always quote the delimiter argument (
-d',') to avoid the shell interpreting special characters like;or|. - When scripting, pin down the exact field number with a quick manual check (
head -1 file | cut -d',' -f2) rather than guessing from eyeballing the file.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Building a list of system usernames for an audit:
cut -d':' -f1 /etc/passwd | sort. - Extracting the PID column from
psoutput to feed intokill:ps -ef | grep myapp | cut -c10-15. - Pulling a single column out of a CSV export for a quick calculation:
cut -d',' -f5 sales.csv | paste -sd+ | bc.
Common interview questions
- What's the difference between
cut -candcut -f?-cselects by raw character position regardless of content;-fsplits the line into fields on a delimiter (set with-d) and selects by field number. - Why might
cutgive wrong results on a CSV file?cutsplits naively on the delimiter character, so commas inside quoted fields break the field alignment; a CSV-aware tool orawkwith proper parsing handles that correctly. - How do you select all fields from the third one onward?
cut -d',' -f3-— an open-ended range with no upper bound selects from field 3 to the end of the line.
Frequently Asked Questions
How do I extract a column from a CSV file?
Use cut -d',' -f2 file.csv to print the second comma-delimited field from every line. For quoted or complex CSV, prefer a real CSV-aware tool since cut treats delimiters naively.
Why did cut print entire lines unchanged when I expected fields?
If a line doesn't contain the delimiter, cut prints it unchanged by default. Use -s to suppress lines without the delimiter, which is useful when filtering mixed input.
What's the difference between -c and -f?
-c selects by raw character position (byte/column offsets), ignoring any delimiter. -f selects by field number after splitting the line on the delimiter set with -d. Use -c for fixed-width data and -f for delimited data.
Can cut select fields in a different order than they appear?
No — cut always outputs fields in their original order regardless of the order you list them in -f. Use awk if you need to reorder fields.
Cheat sheet
Download a quick-reference cheat sheet for cut.