>_cmd.script

cut

Extract columns or fields from each line of input

Text Processing

By CMD Script Team · 4 min read · Last updated

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

Options

Command options and flags
FlagDescription
-dSet the field delimiter (default is TAB)
-fSelect specific fields by number or range, e.g. -f1,3 or -f2-4
-cSelect specific character positions by number or range, e.g. -c1-10
-sSuppress lines that don't contain the delimiter
--complementInvert the selection, printing everything except the specified fields
--output-delimiterUse 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 field
  • cut -c1-5 file.txt — print the first 5 characters of each line
  • cut -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 sort to 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 ps output: ps aux | cut -c1-10
awk -F',' '{print NF}' data.csv | sort -u   # sanity-check field count before cutting

Common mistakes

  • Assuming cut handles 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 -d and 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, since cut doesn't collapse repeated delimiters the way awk does by default.
  • Expecting cut -f3,1 to reorder output — cut always prints fields in original left-to-right order regardless of the order listed.

Tips

  • For space-separated data with inconsistent spacing (like ps aux), prefer awk '{print $2}' over cut, since awk collapses repeated whitespace automatically.
  • Use cut -s when 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 -u before trusting cut -f output on messy CSV data.

Best practices

  • Reach for cut only on genuinely simple, single-character-delimited data; switch to awk once 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 ps output to feed into kill: 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 -c and cut -f? -c selects by raw character position regardless of content; -f splits the line into fields on a delimiter (set with -d) and selects by field number.
  • Why might cut give wrong results on a CSV file? cut splits naively on the delimiter character, so commas inside quoted fields break the field alignment; a CSV-aware tool or awk with 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.