The cut command in Linux is great for snipping sections of a particular file or data and generating a standard result or output. Since it’s a highly powerful command used to manipulate text, you can use the cut command to process your files in various ways.
With that said, if you’d like to learn how to use this powerful command, here’s a cut command in Linux tutorial with examples below.
How the Cut Command in Linux Works
The cut command works by manipulating text in a vertical manner. Most of the time, this Linux command is combined with other commands and filters. Here is what the code generally looks like:
cut [OPTION] [FILE]
You must specify an option, or else the command will return an error. Meanwhile, the File part is the file that you want to manipulate. If you do not specify a file, the command will simply process what’s included in the standard input.
You can primarily use the cut command to cut according to a character, byte, and fields. Furthermore, you can also utilize the cut command if you’re ever complementing a section. For this quick tutorial, go ahead and refer to the capitals.txt file below for the following examples below:
Manila PH 1
Canberra AU 2
WashingtonDC US 3
Use the Cut Command to Cut According to Byte
For this scenario, you will be using the -b option to cut data according to a byte. Here is what the code should look like:
cut -b [LIST] [FILE]
LIST indicates the bytes you want to take per line from the file you’ll use for processing. Now, if you want to simply print the first byte of a file, use the following code below:
cut -b 1 capitals.txt
Use the Cut Command to Cut According to Character
On the other hand, you need to use the -c option to cut a file according to a character. Here is what the code looks like for this scenario:
cut -c [(a)-(b)/(a),(b)/(b)] [FILE]
This time, the LIST here indicates the characters you want to take per line from the file you’re processing. The a symbol here indicates the beginning position of the character, while b indicates the ending position if there is a “-” symbol in the middle.
Without it, the stated numbers only indicate the position you wish to print from your selected file.
Here’s an example code and its corresponding output:
cut -c 1,3,5 capitals.txt
As you guessed, this code printed the first, third, and fifth characters per line from the capitals.txt file.
Use the Cut Command to Cut According to Fields
This cut command is very helpful when you need to get specific information from your file. The code for this is as shown:
cut -f [LIST] [FILE]
And if you want to get the second field from capitals.txt, you should use the following code:
cut -f 2 capitals.txt
You can also use the cut command to perform specific Linux cut using multiple fields. However, the problem when doing this is that the output ends up being concatenated without a delimiter. When doing so, you will need to use the –output delimiter option.
Here is a sample below:
cut -f 1,3 capitals.txt –output-delimiter=’_’
In the example above, the first and third fields are taken from the file. The output delimiter here is an underscore “_” and is used to concatenate the extracted fields from capitals.txt.