Skip to main content

cut

The cut command extracts sections from each line of a file or standard input. It is commonly used to extract columns from a delimited text file like a CSV or /etc/passwd.

Basic Syntax

cut OPTION... [FILE]...

Common Options

OptionDescription
-d, --delimiter=DELIMUse DELIM instead of TAB for field delimiter.
-f, --fields=LISTSelect only these fields; also print any line that contains no delimiter character, unless the -s option is specified.
-c, --characters=LISTSelect only these characters.
-b, --bytes=LISTSelect only these bytes.
--complementComplement the set of selected bytes, characters or fields (select everything EXCEPT the specified parts).

Real-world Examples

  1. Extract the first field from a CSV file:

    cut -d ',' -f 1 data.csv
  2. Extract the username (field 1) from /etc/passwd:

    cut -d ':' -f 1 /etc/passwd
  3. Extract characters 1 through 5 of each line:

    cut -c 1-5 file.txt
  4. Extract multiple fields (1st and 3rd):

    cut -d ',' -f 1,3 data.csv