Linux Cut Command With Examples
cut command for remove sections from each lines of output in bash.
cut command provides mechanism to filter extract column/text from file or standard output. Detailed examples can be found below. We have following text file named
apple 1 good
grape 5 bad
banana 2 not bad
cut OPTION... [FILE]...
Cut command provides ability to select 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 single character.
$ cut -c3 fruits.txt
Select Column For Character Range
In previous example we have printed single character to the terminal. We can also print multiple characters to the console by specifying character range like below. We can see that character index starts from 1.
$ cut -c1-6 fruits.txt
Select Column Using Start Position
Another useful feature of cut is specifying only 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
Field is some part of text delimited with specific characters like space,tab, double comma etc. We can select text with field number. By default field delimiter is tab. Our text is all ready provides 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
Following example selects more than one field one by one not using ranges.
$ cut -f1,2,3 fruits.txt
One of most popular usage of
cut is printing last field. The problem is that field count may change according to text and we can not specify last field only using
cut command. 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 acorrding 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 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
Delimiter is used to divide fields. By default tab is used as delimiter. But it can be specified with
-d option like below. Following example we use
: as delimiter because the
passwd file uses it. Then we only want to show user names those 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 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. Following example will change tab delimiter to the comma.
$ cut -f1,2,3 --output-delimiter=',' fruits.txt