The “cut” Command in Linux [8 Practical Examples]

The cut command in Linux is a utility tool for extracting a range of information from a file. It cuts out specific sections from each line of the file. The cut command by default displays extracted contents on your Terminal. In this article, I will demonstrate the basic applications of the cut command with several practical examples.

A. Description

Using the cut command in Linux you can slice texts based on byte/s, character/s, field/s, or delimiters. You can apply these parameters with the help of the options. You must type at least one of the available options after the cut command. Otherwise, the system will show error messages.

B. Syntax

The syntax of the cut command in Linux simply contains single or multiple options and then the desired Filename.

cut OPTION... [FILE]...
Note: In the above syntax the 3 dots after each square bracket indicate that multiple OPTIONs and URLs can be used at a time.

C. Options

The cut command in Linux offers several options to modify your experience of extracting information from files. However, you will find the most useful one’s here for your convenience. For any further features, you can always look at the man page.

man cut

Useful Options

  • -b/–bytes ( Extracts assigned number of bytes from file )
  • -c/–characters ( Extracts assigned number of characters from file )
  • -d/–delimiter ( Extracts contents between assigned delimiters from file )
  • -f/–fields ( Extracts assigned number of fields from file )

NB: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them. Moreover, The options mentioned above accept only one type of range at a time. Types of ranges are defined as follows.

  • N- ( Extracts only the Nth integer, counting starts from 1 )
  • N-M ( Extracts from Nth integer to Mth integer )
  • M- ( Extracts from Mth integer to the end of the file )

Practical Examples of the “cut” Command in Linux

With the cut command in Linux, you will be able to pull out vital contents from a specific file as well as from the system. Here are some practical examples of the cut command to enhance your experience in Linux.

Example 1: Extracting a Specific Field Using the “cut” Command in Linux

You can extract a specific field from a file using the cut command in Linux. For this, you will need to use the option -f. In this example, I will extract the first field from my text file “employee.txt”. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cut -f 1 employee.txt

➌ Now, press the ENTER button.

Output >

In the image below, you can see that I have extracted the first field of my text file using the cut command in Linux. To view all the contents of the file you can use the cat command.Extracting 1st field of a text file using cut command in linux.


Similar Readings


Example 2: Extracting Multiple or a Range of Fields Using the “cut” Command in Linux

Using the cut command in Linux with the -f option you can cut out multiple fields from a file. In this example, I will demonstrate a few possible ways to extract multiple fields from the file “employee.txt”. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cut -f 2,3,4 employee.txt

Or,

cut -f 2-4 employee.txt

Or,

cut -f 2- employee.txt

➌ Now, press the ENTER button.

Output >

To view all the contents of the file you can use the cat command. In the first image, you can see that I explicitly typed a list of fields separated by a comma (,) to print out multiple fields.Extracting multiple fields ofrom a file with listed range using cut command in linux.You can see in this second image, I have passed a range of values to the option to extract multiple fields.Extracting multiple fields with a range of values using cut command in linux.In this third approach, I used the Hyphen (-) after a field value which indicates all the values after the 2nd field.Extracting multiple fields of a file from a givenvalue till the end of a line operator using cut command in linux.

Example 3: Cutting Out a Specific Number of Bytes Using the “cut” Command in Linux

You can extract a specific number of bytes from a file using the cut command in Linux. For this, you will need to use option -b. In this example, I will extract the first 5 bytes of each line from my text file “employee.txt”. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cut -b -5 employee.txt

Or,

cut -b 1,2,3,4,5 employee.txt

➌ Now, press the ENTER button.

Output >

In the image below, you can see that I extracted the first 5 bytes of each line from my text file with a prefixed Hyphen (-)  using the cut command in Linux. I have used the cat command to show you the contents of the file.Printing first 5 bytes of each line using cut command in linux.In this second image, I applied the -b option with a list of bytes to get the same output as before.Printing first 5 bytes of each line with a list using cut command in linux.


Similar Readings


Example 4: Cutting Out a Range of Bytes Using the “cut” Command in Linux

Using the cut command in Linux with the -b option you can cut out a range of bytes from a file. In this example, I will print out from 25th to the last byte of the file “employee.txt”. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cut -b 25-33 employee.txt

Or,

cut -b 25- employee.txt

➌ Now, press the ENTER button.

Output >

To view the file’s contents, you can use the cat command. In the first image, you can see that I extracted all the bytes from the 25th byte in my file typing the range ( 25 – 33 ).Cutting out All the contents from 25th byte till using the cut command in linux.In this 2nd image, I used the Hyphen (-) after 25 which extracted all the values after the 25th byte.Printing from 25th byte till the end of each line using cut command in linux.

Example 5: Cutting Out a Specific Number of Characters Using the “cut” Command in Linux

You can extract a specific number of characters from a file using the cut command in Linux. For this, you will need to use option -b. In this example, I will extract the first 3 characters of each line from my text file “employee.txt”. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cut -c -3 employee.txt

Or,

cut -c 1,2,3 employee.txt

➌ Now, press the ENTER button.

Output >

In the image below, you can see that I extracted the first 3 characters of each line from my text file with a prefixed Hyphen (-)  using the cut command in Linux. I have used the cat command to show you the contents of the file.Printing 1st 3 characters of each line.In this second image, I applied the -c option with a list of bytes to get the same output as before.Extracting 1st 3 characters of each line with listed values.

Example 6: Cutting Out a Range of Characters Using the “cut” Command in Linux

Using the cut command in Linux with the -c option you can cut out a range of characters from a file. In this example, I will print out from 25th to the last character of the file “employee.txt”. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cut -c 25-33 employee.txt

Or,

cut -c 25- employee.txt

➌ Now, press the ENTER button.

Output >

To view the file’s contents, you can use the cat command. In the first image, you can see that I extracted all the characters from the 25th character in my file typing the range ( 25 – 33 ).Extracting all the characters after the 25th character from a file.In this 2nd image, I used the Hyphen (-) after 25 which extracted all the characters after the 25th character.Extracting all the characters after the 25th character from a file.


Similar Readings


Example 7: Extracting Contents Between Delimiters of a Field Using the “cut” Command in Linux

You can extract contents between delimiters from a file using the cut command in Linux. For this, you will need to use option -d. In this example, I will extract the values between “:” of the 4th field from my text file “employee.txt”. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cut -d “:” -f 4 employee.txt

➌ Now, press the ENTER button.

Output >

In the image below, you can see that I have extracted the values between “:” of the 4th field of my text file using the cut command in Linux. To view the file’s contents, you can use the cat command.Extracting the values between “:” of the 4th field of text file using the cut command in Linux

Example 8: Extracting Contents Between Delimiters of Multiple Fields Using the “cut” Command in Linux

Using the cut command with the -d option you can cut out contents between delimiters of multiple fields of a file. In this example, I will extract the texts between “:” from the last three fields of the file “employee.txt”. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cut -d “:” -f 4 employee.txt

➌ Now, press the ENTER button.

Output >

To view the file’s contents, you can use the cat command. In the image below, you can see that I printed out texts between the “:” delimiter from multiple fields.Printing out texts between the “:” delimiter from multiple fields.

Conclusion

In this writing, I presented the frequent uses of the cut command in Linux. Keep in mind that you must provide a file or redirect standard input to use this command. I hope these practical examples will help you retrieve necessary information from the command line and increase your expertise in Linux.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Anonnya Ghosh

Hello there! I am Anonnya Ghosh, a Computer Science and Engineering graduate from Ahsanullah University of Science and Technology (AUST). Currently, I am working as a Linux Content Developer Executive at SOFTEKO. The strong bond between Linux and cybersecurity drives me to explore this world of open-source architecture. I aspire to learn new things further and contribute to the field of CS with my experience. Read Full Bio

Leave a Comment