Sunday 9 July 2017

Cut command usage and examples

Cut 
Cut is used for text extraction. Like its name it cuts given number of characters or field from specified file. The cut command assumes that the fields are separated by tab character. If the fields are delimited by some other character, then option –d is used to set delimiter.

Syntax
cut [options] file_name

Options
-b Select only the bytes from each line as specified in LIST.
-c Select only the characters from each line as specified in LIST.
-d Used for the field delimiter.
-f Select only mentioned fields on each line; also print any line that contains no delimiter character.
--complement Select All Fields Except the Specified Fields.
-s
Do not print lines not containing delimiters.
--output-delimiter=STRING Set STRING as the output delimiter string.

LIST specifies a byte/character, a set of bytes/characters, or a range of bytes/characters. Each LIST is made up of an integer, a range of integers, or multiple integer ranges separated by commas.

c Nth byte, character, or field, counted from 1.
N- From the Nth byte, character, or field, to the end of the line.
N-M From the Nth to the Mth byte, character, or field (inclusive).
-M From the first to the Mth byte, character, or field.

Let us consider below skills.txt file for understanding cut examples.

cat skills.txt
Barry likes coding.
Henry like to travel.
Joseph is an dancer.
Richi is an actress.
Peter was journalist, now he is a manager.   
     
Example               

Command to display specific column from a file

cut -c3 skills.txt 
r
n
s
c
t

The above example displays third character from each line of the file skills.txt.

Command to display specific range of column from a file

cut -c1-4 skills.txt
Barr
Henr
Jose
Rich
Pete

The above example displays character from range 1 to 4 from each line of the file.

Command to display columns from a file beginning from specified Start Position

cut -c4- skills.txt
ry likes coding.
ry like to travel.
eph is an dancer.
hi is an actress.
er was journalist, now he is a manager.

The above example displays starting from 4th column till the last column of each line of the file.

Command to display columns from a file until specified End Position

cut -c-7 skills.txt
Barry l
Henry l
Joseph
Richi i
Peter w

The above example displays columns from 1st to the 7th column of each line of the file.

To display specific field from a file

When we want to display a whole field, need to use option -f and -d. The option -f specifies which field you want to extract, and the option -d specifies the field delimiter that is used in the input file.

cut -d' ' -f2 skills.txt
likes
like
is
is
was

The above example displays the second field in each line by treating the space as delimiter. 

To display multiple fields from a file

We can print more than one field by specifying the position of the fields in a comma delimited list.

cut -d' ' -f2,3 skills.txt
likes coding.
like to
is an
is an
was journalist,
The above example displays 2nd and 3rd fields only. 

To display range of fields from a file

cut -d' ' -f1-2 skills.txt
Barry likes
Henry like
Joseph is
Richi is
Peter was

The above example prints the first and second fields.

To display fields from beginning to the end field specified

In order to print the first two fields, you can ignore the start position and specify only the end position.

cut -d' ' -f-3 skills.txt
Barry likes coding.
Henry like to
Joseph is an
Richi is an
Peter was journalist,

The above example prints up to 3rd field. 

Display fields from start field specified till the ending.

To print the fields from second fields to last field, you can remove the last field position.

cut -d' ' -f2- skills.txt
likes coding.
like to travel.
is an dancer.
is an actress.
was journalist, now he is a manager.

The above example displays fields from 2nd to the last field of each line of the file.

Display fields only when line contains the delimiter

cut -d'#' -f2- skills.txt
Barry likes coding.
Henry like to travel.
Joseph is an dancer.
Richi is an actress.
Peter was journalist, now he is a manager.

In the above example, we have specified the delimiter as #, and cut command displays the whole line, even when it doesn’t find any line that has # as the delimiter.

We need to use -s option to display the lines that contains the specified delimiter.
cut -d'#' -s -f2- skills.txt

No output shown. 

Display all fields except the specified fields

In order to hide certain field use option --complement.

cut -d' ' --complement -f2 skills.txt
Barry coding.
Henry to travel.
Joseph an dancer.
Richi an actress.
Peter journalist, now he is a manager.

The above example displayed all the fields except 2nd field from the file.
For Display changing the output delimiter

By default the output delimiter is same as input delimiter that we specify in the cut -d option. To change the output delimiter use the option --output-delimiter as shown below.

cut -d' ' --complement -f2 --output-delimeter='|' skills.txt
Barry|coding.
Henry|to|travel.
Joseph|an|dancer.
Richi|an|actress.
Peter|journalist,|now|he|is|a|manager. 

The above example has the input delimiter as space , but the output delimiter is replaced by | (pipe).
When we set the –output-delimiter value to $’\n’ then each and every field of the cut command output is displayed in a separate line.

TIPS

The entire file is displayed when you don’t specify a number before or after the ‘-‘.

When calling cut command, use the -b, -c, or -f option, but only one of them.

If no FILE is specified, cut reads from the standard input.

No comments:

Post a Comment