cut command for remove sections from each line of output in bash.
cut command provides a mechanism to filter extract column/text from a file or standard output. Detailed examples can be found below. We have the following text file named
apple 1 good grape 5 bad banana 2 not bad
cut OPTION... [FILE]...
Cut command provides the ability to select a specified column according to character number for all rows. Here we will print all rows 3 character line by line to the console. This will only print a single character.
$ cut -c3 fruits.txt
Select Column For Character Range
In the previous example, we have printed a single character to the terminal. We can also print multiple characters to the console by specifying the character range like below. We can see that the character index starts at 1.
$ cut -c1-6 fruits.txt
Select Column Using Start Position
Another useful feature of cut is specifying only to start position up to end.
$ cut -c3- fruits.txt
Select Column Using End Position
We can print range from start to the specified position only specifying end position. This will assume start position as 1
$ cut -c-3 fruits.txt
Select Single Field
The field is some part of text delimited with specific characters like space, tab, double comma, etc. We can select text with field numbers. By default field delimiter is tab. Our text is already provided tab for delimitation. In this example, we will select 3. field.
$ cut -f3 fruits.txt
Select Multiple Fields
As we do in characters we can also specify field range. We will select fruit names and counts with the following command.
$ cut -f1-2 fruits.txt
The following example selects more than one field one by one not using ranges.
$ cut -f1,2,3 fruits.txt
One of the most popular usages of
cut is printing last field. The problem is that field count may change according to text and we can not specify the last field only using
cut command. The following example uses
rev command to get last field.
$ cat fruits.txt | rev | cut -f1 | rev
Select Fields Include Specified Chars
cut do not have the ability to exclude lines according to characters but
grep can be used easily to implement this feature. We will only select rows contain
$ grep 'app' fruits.txt | cut -f1-2
Select Fields Except for Specified Chars
We can reverse the previous example and remove unwanted rows. We will remove lines contains
app and then print columns from 1 to 2 .
$ grep -v 'app' fruits.txt | cut -f1-2
The delimiter is used to divide fields. By default, the tab is used as the delimiter. But it can be specified with
-d option like below. Following the example, we use
: as delimiter because the
passwd file uses it. Then we only want to show user names that resides in field 1.
$ cut -d: -f1 /etc/passwd
Print Except Fields
As we see previously fields can be printed as ranges too. We can also print by excepting fields. This will print all renaming fields. Following the example, we can want to print all fields except 2.
Text may have delimiters by default. In some situations, we may want to change delimiter while printing to the console.
--output-delimiter option can be used for this operation. The following example will change the tab delimiter to the comma.
$ cut -f1,2,3 --output-delimiter=',' fruits.txt