“cut” — A Soft Scissor For The Terminally Nerdy

cut — the column extractor from a line of text might sound plain, but when combined with other text processing tools such as grep and sed, its role is anything but plain. Let’s take a look at its elemental operations.

Cut by Character

Cutting specific characters from a line of text is achieved via the -c option the argument of which is a number that specifies the position of the text. The number can also be in the form of a range or a list of comma separated values.

$ echo 'bel' | cut -c 1
b

$ echo 'bel' | cut -c 1-2
be

$ echo 'bel' | cut -c 1,3
bl

Supplying a file to cut will have the same effect, but on every line of it.

$ cut -c 1-8 hello.c
#include

int main
{
printf(
return
}

We can select a column based on either the start or end position via the hyphen (‘ - ‘) special character.

# select the first five characters
$ cut -c -5 hello.c
#incl

int m
{
	prin
	retu
}


# select everything from the second character
$ cut -c 2- hello.c
include<stdio.h>

nt main()

printf("Hello World\n");
return 0;

Cut by Delimiter

The most common use case of cut is to parse the lines by the specified delimiter -d 'DELIM', and fetch the fields of interest -f num. For example, consider the following CSV file that has records of employee details.

$ cat employees.csv 
Ravi,Engineer,26,Bangalore
Mohan,Doctor,23,Bankok
Naveen,Writer,31,Singapore

The name of the employees can be selected like so:

$ cut -d ',' -f 1 employees.csv
Ravi
Mohan
Naveen

To select both name and place of the employees, multiples fields have to be specified.

$ cut -d ',' -f 1,4 employees.csv
Ravi,Bangalore
Mohan,Bankok
Naveen,Singapore

Complement cut

To return not the cut part but the leftover, --complement switch should be specified along with the other options.

$ cut -d ',' -f 1 --complement employees.csv
Engineer,26,Bangalore
Doctor,23,Bankok
Writer,31,Singapore

As can be seen, because of the --complement switch, cut returns everything but the field of interest.

Miscellaneous Options

By default cut returns everything if the specified “cut criteria” fails. This can be silenced through the -s switch.

$ cut -d ':' -f 1 employees.csv
Ravi,Engineer,26,Bangalore
Mohan,Doctor,23,Bankok
Naveen,Writer,31,Singapore

$ cut -d ':' -f 1 -s employees.csv
$

The delimiter in the cut output can be changed via the --output-delimiter option.

$ cut -d ',' -f 1 --complement employees.csv
Engineer,26,Bangalore
Doctor,23,Bankok
Writer,31,Singapore

$ cut -d ',' -f 1 --complement --output-delimiter '#' employees.csv
Engineer#26#Bangalore
Doctor#23#Bankok
Writer#31#Singapore
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s