In Linux, the cut command extracts specific fields or columns from a file or standard input. It can be used to extract parts of a line by specifying the field(s) that you want to include. You can also use it to extract a specific range of characters from each input line.
This article explains the working with some practical examples and different options to filter the data using the cut command. The following content is covered in this guide:
- How to Use the cut Command in Linux?
- Example 1: Cut by Bytes
- Example 2: Cut by Characters
- Example 3: Cut a Specific Field
- Example 4: Compliment a Specific Selection
- Example 5: Define a Delimiter in the Output
Let’s get started!
How to Use the cut Command in Linux?
The syntax of the cut command is described below for your better understanding.
cut [option…] [file…]
Here’s a table of arguments that can be used with it, and you can only use either one of the following at a time.
|-b (–bytes=LIST)||Select/cut by specifying either a byte, set, or range of bytes|
|-c (–characters=LIST)||Select/cut by specifying either a character, set, or range of characters|
|-f (–fields=LIST)||Select/cut by specifying either a field, set, or range of fields|
Other arguments, as below, can also be used with the ones in the table above.
|-d||To specify the delimiter other than the default (Tab)|
|–complement||To display everything except the selected bytes, characters, or fields.|
|–output-delimiter||Allows the user to use a specified output delimiter|
Now, let’s understand the usage of the cut command in the next section.
This section intends to provide the possible usages of the cut command in Linux via examples. Each example has its distinct application. Let’s dig into them one by one:
Example 1: How to Cut by Bytes?
We’d use “-b” as an argument with the following format to cut characters, either a single, multiple, or a range.
cut -b [LIST] [file]
Here, we will use “test” as our file to extract bytes using this command.
$ cut -b 1 test.txt $ cut -b 1-3,5-7 test
We used the “cat” command to open the file. While in the “cut” command, we extracted the first byte from all file lines. Here ‘-b’ represents the byte argument.
In the first command, ‘1’ is the location of each byte concerning the line, which prints the first byte of each line.
While in the second command, the bytes at the fourth and sixth index are cut and display all other bytes.
Example 2: How to Cut by Characters?
It is almost the same as Cut by Bytes discussed above, but here it requires the character’s position with the following syntax.
$ cut -c [LIST] [file]
Let’s test it in the terminal as follows.
$ cut -c 3- test $ cut -c 1-10 test
In the first command, we discard the first two characters and start printing from the third character. You can see that the first two characters from each column are missing.
We are printing the first 10 characters from each column in the second command. For example, the total characters in ‘Employee_Name’ are 13, so it is printing only the first 10 characters and discarding the last 3, which are ‘ame’.
Similarly, in the second column, it is only printing ‘John Lon’, and here space is taking 3 characters which makes it a total of 10 characters, and the same for others as well.
Example 3: How to Cut a Specific Field?
The field is helpful for a fixed number of lines, but what if there is no fixed number? For that, Delimiter works like a separator which is, by default, the “TAB character”. Here is how you can use it to get data.
$ cut -d "delimiter" -f (field number) file.txt
The delimiter needs to be defined by the user; otherwise, you will see the whole line. Here, we will use empty (“ “) space as our delimiter.
$ cut -d " " -f 1 test
Everything is cut here after a single space because the empty space is our delimiter.
Example 4: How to Compliment a Specific Selection?
The complement of a selection displays everything except a byte, character, or field at the position specified by the user, and it is used as follows.
$ echo "Lorem-ipsum dolor sit amet" | cut -d ' ' -f 2
Here, we have specified the second empty space, excluding which the rest of the characters are cut and not displayed.
Example 5: How to Define a Delimiter in the Output?
The cut command concatenates anything without a delimiter which confuses while reading data, so we have the “—output-delimiter” command to define a custom delimiter in output. Here we are going with ‘-‘as our delimiter.
$ cut test -f 1,3 --output-delimiter='_'
Here, we have used ‘_’ as our delimiter to concatenate, which is also shown in the output.
The cut command in Linux is quite helpful for data manipulators because they can cut, add, or separate data according to their choices. This post has provided a detailed explanation of the working/usage of the cut command in Linux. For better understanding, examples are illustrated to show the practical usage of the cut command in Linux.