How to use cut command in Linux?

Cut command is one of the text-filtering tools that is present in both Linux and Unix. Cut command is used to extract specific columns from the lines of text. You pass text using files or pipe the output of another command to the cut command and it prints the data to standard output. Here, you cut a line by delimiter, character and byte.

Cut command is useful for processing CSV files, log files and files having fixed-width fields.

What is delimiter?

Before you take a look at the syntax of cut command, it is necessary for you to understand the concept of the delimiter. The delimiter is a space, tab, comma, colon or any other character that is used to separate words in a line. For example, a colon is used as a delimiter in /etc/passwd file for separating different values. In CSV files, comma is used as a delimiter.

Cut command considers tab as a default delimiter. So if your file is having tab as a delimiter then no need to use -d option with cut command.

Syntax of cut command

cut options [file]

You can also pass more than one files to cut command. There is no need to specify the file if you are passing the output of another command or providing input from the standard input.

Using options you specify whether to use delimiter, character or byte for cutting the lines. Following are the options that are important and useful for you-

  • -f(--fields) - Here, you specify the fields that you want to extract.
  • -c(--characters) - Here, you specify the characters that you want to extract.
  • -b(--bytes) - Here, you provide the bytes that you want to extract.

Note: You cannot use all the above three options together, at a time you can use only one of the above options. The numbering of fields, characters or bytes starts from 1.

You can pass any one of the five values to -f, -c and -b options-

  • Integer
  • Multiple integers separated by commas.
  • A range of integers.
  • Multiple ranges of integers separated by commas.
  • Combination of multiple integers and multiple range of integers.

Values are provided in the following ways-

Value Description
N extracts Nth field, character or byte.
N, M, O, P extracts Nth, Mth, Oth and Pth field, character or byte.
N- extracts from the Nth field, character or field to the end of the line.
N-M extracts from Nth to Mth field, character or byte.
-M extracts from the first to Mth field, character or byte.
N-M, O, P or O, N-M, P extracts from the Nth to Mth, Oth and Pth field, character or byte.
  • -d(--delimiter) - Here, You specify the delimiter that you want to use with cut command. By default, the tab is considered a delimiter.
  • --complement - It is used to get columns that are not specified by -f, -c or -b options.
  • --output-delimiter - By default, cut command uses input delimiter as an output delimiter but you can change this behaviour using this option.

How to cut by field

Using -f option you can specify the fields that you want to extract. In the below examples, we will use the following two files. In employees.txt file, fields are separated by TAB. And in students.txt fields are separated by a comma.

E01	Geeta Sahu	$4000	Chairman
E02	Kaushal Sahu	$2100	Vice-President
E03	Mohit Natani	$1500	Administrator

To display 2nd and 4th field from employees.txt file, you should type:

$cut -f 2,4 employees.txt
Geeta Sahu  Chairman
Kaushal Sahu    Vice-President
Mohit Natani    Administrator

If you want to display from 1st to 3rd field, then type this:

$cut -f -3 employees.txt
$cut -f 1-3 employees.txt
01 Geeta Sahu  $4000
E02 Kaushal Sahu    $2100
E03 Mohit Natani    $1500

To display from 2nd to the last field, you should type:

$cut -f 2- employees.txt
$cut -f 2-4 employees.txt
Geeta Sahu  $4000   Chairman
Kaushal Sahu    $2100   Vice-President
Mohit Natani    $1500   Administrator

How to cut based on a delimiter

If a file uses delimiter other than TAB then use -d option to specify the delimiter used by the file.

For example, to get the 2nd field from students.txt that use comma as a delimiter, type the below command:

$cut -d ',' -f 2 students.txt

How to complement the selection in cut command

Suppose, you want to select fields that are not specified with the -f option, in that case, use --complement option.

The following command will print all the fields except 1st.

$cut -f 1 --complement employees.txt
Geeta Sahu  $4000   Chairman
Kaushal Sahu    $2100   Vice-President
Mohit Natani    $1500   Administrator

How to specify output delimiter

Use --output-delimiter to specify the output delimiter. To set hypen(-) as an output delimiter, run the below command:

$cut -f 2,4 --output-delimiter='-' employees.txt
Geeta Sahu-Chairman
Kaushal Sahu-Vice-President
Mohit Natani-Administrator

How to cut by characters and bytes

Each ASCII character occupies 1 byte whereas characters in Unicode need 1 byte to 4 bytes to represent it.

Most of the time, you will find that -b and -c options produce the same result. Let's understand this with the help of some examples-

Select the characters or bytes from 7 to 12:

$echo 'Shell Scripting' | cut -b 7-12
$echo 'Shell Scripting' | cut -c 7-12

Select the 3rd character or byte:

$echo 'Shell Scripting' | cut -b 3
$echo 'Shell Scripting' | cut -c 3

Select the 2nd, 3rd, 4th and 11th character or byte:

$echo 'Shell Scripting' | cut -b 2,3,4,11
$echo 'Shell Scripting' | cut -c 2,3,4,11

Cut examples

In general, cut command is used with other commands such as sort, sed, awk and others through piping to obtain the desired result.

For example, to obtain a list of all users run this command:

$cut -d':' -f 1 /etc/passwd

To know more about this, visit Linux get all users

You can find more example at cut command examples