Skip to content

Cut Command

In Bash, the cut command is useful for dividing a file into several smaller parts.

Suppose you have a file that looks like this

Terminal window
John Smith 31
Robert Jones 27
...

This file has 3 columns separated by spaces. To select only the first column, do the following.

Terminal window
cut -d ' ' -f1 filename

Here the -d flag, specifies the delimiter, or what separates the records. The -f flag specifies the field or column number. This will display the following output

Terminal window
John
Robert
...

Sometimes, it’s useful to display a range of columns in a file. Suppose you have this file

Terminal window
Apple California 2017 1.00 47
Mango Oregon 2015 2.30 33

To select the first 3 columns do

Terminal window
cut -d ' ' -f1-3 filename

This will display the following output

Terminal window
Apple California 2017
Mango Oregon 2015
  • cut [option] file
OptionDescription
-b LIST, --bytes=LISTPrint the bytes listed in the LIST parameter
-c LIST, --characters=LISTPrint characters in positions specified in LIST parameter
-f LIST, --fields=LISTPrint fields or columns
-d DELIMITERUsed to separate columns or fields