Linux Cut Command With Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-cut-command-examples/
PINTEREST
PINTEREST
INSTAGRAM

Linux provides 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 fruits.txt

Syntax

Select Column

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.

Select Column

Select Column

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.

Select Column For Character Range

Select Column For Character Range

Select Column Using Start Position

Another useful feature of cut is specifying only start position up to end.

Select Column Using Start Position

Select Column Using Start Position

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

Select Column Using End Position

Select Column Using End Position

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.

LEARN MORE  How To Cut Existing Network Connection In Linux?

Select Single Field

Select Single Field

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.

Select Multiple Fields

Select Multiple Fields

Following example selects more than one field one by one not using ranges.

Last Field

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.

Last Field

Last Field

Select Fields Include Specified Chars

By default 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 app

Select Fields Include Specified Chars

Select Fields Include Specified Chars

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 .

Select Fields Except Specified Chars

Select Fields Except Specified Chars

Specify Delimiter

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.

LEARN MORE  Nohup Resume Command Exit After Exit

Specify Delimiter

Specify Delimiter

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 .

Change Delimiter

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.

Change Delimiter

Change Delimiter

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-cut-command-examples/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)