BASH Programming

Linux cut command examples

Linux `cut` command is one of the useful commands to work with string data and file content. It is mainly used for cutting content from tabular data or CSV file. Each line of the file can be sliced and the required data can be retrieved easily by using `cut` command. It can also be used to cut text by delimiter or character or byte position. This command has many options to cut data from the text or file in different ways. How cut command can be used with different options is shown in this tutorial by using various examples.


cut OPTION... [FILE]

OPTION value is mandatory to use for `cut` command and FILE name is optional. If you omit the file name in the command then it will take data from standard input. But if you omit the OPTION value in cut command then it will generate the error.


Option tag Description
-b or –byte=LIST Select the particular bytes.
-c or –character=LIST Select the particular characters.
-d or –delimiter=DELIM Select DELIM value as delimiter. By default delimiter is TAB.
-f or –fields=LIST Select the particular fields
–complement Use to complement the output
-s or –only-delimited Omit the lines that don’t contain delimiter.
–output-delimiter=STRING Use STRING value as output delimiter.
-z or –zero-terminated Use NULL as line delimiter.

Example-1: Cut by bytes

The following cut command will slice the standard input data based on the defined bytes. Here, 3,4,5 and 6 are defined as bytes. The output will generate based on the standard input. Press CTRL+D to exit from the command.

$ cut -b 3,4,5,6


Here, input is “I like programming” and the output is “like” based on the bytes mentioned.

Create a text file named “productlist.txt” with the following content for applying the `cut` command. Use TAB to separate the fields of the file content.

ID   Name     Price
01   Pen       $2
02   Pencil    $1.5
03   Eraser    $1

Run the following command to retrieve only the product ID. Here, bytes are given as a range, ‘1-2′ to cut the data.

$ cut -b 1-2 < productlist.txt


Example-2: Cut by characters

You can generate the same output by using -c option, those are shown in the previous examples. So, there is no particular difference between the output of -b and -c options of the cut command. Run the following command to show the use of -c option. Here, 4- is used as an option value that will cut from position 4 to all the remaining characters of each line. You can also use the negative sign in front of the value like -4, then it will cut from the beginning to 4 positions.

$ cut -c 4- productlist.txt


First three characters are omitted in the output.

Example-3:  Cut by delimiter and fields

Create a CSV file named ‘students.csv’ with the following content to show the use of delimiter.

1003,Asraful Haque,BBA,3.85
1004,Momotaj Khan,English,3.20

Run the following command to print the student’s name and CGPA from the students.csv file. According to the file content, the 2nd and the 4th fields contain the student name and CGPA. So, two options are used in this command to show the output. One is the delimiter, -d, which is ‘,’ here and another is fielding option, -f.

$ cut -d  ',' -f 2,4 students.csv


If you want to print two or more sequential columns of any tabular data or CSV file then you can define the fields as a range. The range of field value is used in the following command. Here, all the fields from 2 to 4 will print as output.

$ cut -d  ',' -f 2-4 students.csv


Example-4:  Cut by complement

–complement option is used to generate the opposite output of the command. In the following command, the productlist.txt file is used which is created at the first example.  Here, -f option will cut the 1st field and –-complement option will print the other all fields of the file by omitting the 1st  field.

$ cat productlist.txt
$ cut --complement -f 1 productlist.txt


Example-5:  Cut by output-delimiter

This option is used to generate output by using a particular delimiter. The previously created students.csv file is used in the following command. ‘,’ is the default delimiter of any CSV file. The following command uses ‘:’ as output delimiter value.

$ cat students.csv
$ cut -d "," -f 1-3 students.csv --output-delimiter=":"


After executing the command, input delimiter ‘,’ will be replaced by the output delimiter ‘:’ and the first three fields will be printed by separating ‘:’.

Example-6: Using cut with pipe command

Cut command can take input from any file or from the user input. But input text can be sent to cut command by using the pipe. The following command shows the use of cut command with the pipe. Here, echo command will the input text to cut command and cut command will cut the fields 2 and 3 based on the delimiter.

$ echo "Testing cut command" | cut -d " " -f 2,3


Example-7: Save cut command output to a file

If you want you can also save the cut command output into any text file. The following command will take the content of the students.csv file as input, cut the 2nd field based on the delimiter and print the output in a text file named ‘student_names.txt’.

$ cat students.csv
$ cat students.csv | cut -d cat students.csv | cut -d "," -f 2 > student_name.txt“,”
f 2 > student_name.txt
$ cat student_names.txt



Most common uses of cut command are tried to explain in this tutorial by using the above examples. Hope, the uses of cut command will be cleared of the readers after exercising the above examples.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.