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]...
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.
Similar Readings
- The “grep” Command in Linux [10+ Practical Examples]
- The “wc” Command in Linux [15 Practical Examples]
- The “sort” Command in Linux [16 Practical Examples]
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.You can see in this second image, I have passed a range of values to the option to extract multiple fields.In this third approach, I used the Hyphen (-) after a field value which indicates all the values after the 2nd field.
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.In this second image, I applied the -b option with a list of bytes to get the same output as before.
Similar Readings
- The “nano” Command in Linux [13 Practical Examples]
- The “jed” Command in Linux [8 Practical Examples]
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 ).In this 2nd image, I used the Hyphen (-) after 25 which extracted all the values after the 25th byte.
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.In this second image, I applied the -c option with a list of bytes to get the same output as before.
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 ).In this 2nd image, I used the Hyphen (-) after 25 which extracted all the characters after the 25th character.
Similar Readings
- The “vi” Command in Linux [6 Practical Examples]
- The “vim” Command in Linux [8 Practical Examples]
- The “egrep” Command in Linux [10+ Practical Examples]
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.
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.
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
- The “paste” Command in Linux [6 Practical Examples]
- The “split” Command in Linux [6 Practical Examples]
- The “sed” Command in Linux [7 Practical Examples]
- The “tr” Command in Linux [6 Practical Examples]
- The “uniq” Command in Linux [6 Practical Examples]
- The “printf” Command in Linux [8 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners