Linux Cut Command With Examples – POFTUT

Linux Cut Command With Examples

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

Example Text

Example Text

Syntax

Select Column

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.

Select Column

Select Column

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.

Select Column For Character Range

Select Column For Character Range

Select Column Using Start Position

Another useful feature of cut is specifying only to 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

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.

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.

LEARN MORE  How To Find Files, Folders and Directories In Linux From Command Line?

Select Multiple Fields

Select Multiple Fields

The following example selects more than one field one by one not using ranges.

Select Multiple Fields

Select Multiple Fields

Last Field

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.

Last Field

Last Field

Select Fields Include Specified Chars

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

Select Fields Include Specified Chars

Select Fields Include Specified Chars

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 .

Select Fields Except Specified Chars

Select Fields Except for Specified Chars

Specify Delimiter

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.

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 the example, we can want to print all fields except 2.

LEARN MORE  Nmap Output

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. The following example will change the tab delimiter to the comma.

Change Delimiter

Change Delimiter

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)