Back to the command reference

cut

ShellFundamentals In simulator In real Linux

Select fields or characters from each line

Name: cut

You can practise it right away in the simulator, and use it in real Linux mode too.

Description

cut extracts parts of each line: fields with -f (separator set by -d, tab by default), character positions with -c or bytes with -b. It suits data with a fixed delimiter, such as CSV or /etc/passwd; for irregular whitespace awk is a better fit.

Syntax

cut OPTION... [FILE]...

Common options

-d / --delimiter DELIMField delimiter (default: tab)
-f / --fields LISTFields to select, e.g. 1,3 or 2-4
-c / --characters LISTCharacter positions, e.g. 1-5
-b / --bytes LISTSelect bytes, e.g. -b 1-4
-s / --only-delimitedSkip lines without the delimiter
--complementPrint everything except the selection
--output-delimiter STRINGJoin the output fields with STRING

Examples

  • $ cut -d, -f1 data/scores.csv

    First column of a CSV

    Explain
  • $ cut -d: -f1 /etc/passwd

    List all user names on the system

    Explain
  • $ cut -c1-10 logs/app.log

    First 10 characters of each line (the date)

    Explain

Related