Saturday, July 26, 2014

cut command in linux/unix

A filter, such as the UNIX cut command, is a program that processes an input stream of data to produce an output stream of data. The input data may be fed into the program's standard input or read from a file, and the output data may be sent to standard output or sent to a file.

The UNIX cut command is used to extract a vertical selection of columns (character position) or fields from one or more files. The syntax for extracting a selection based on a column number is:

$ cut -c n [filename(s)]

Here c tells us that we have to slice a column, and n is the number of the column.
Note : that column number start with number 1 rather than 0.

Example  1 - Cut column 1

 $ cat class

 A       Johnson    Sara
 D       Smith      Tom
 B       Jones      Mindy
 D       Anderson   Bob

         

The following UNIX cut example will extract the first column of the class file:

 $ cut -c 1 class

   A
   D
   B
   D

       
Extracting the field number
The syntax for extracting a selection based on a field number is:

 $ cut -f n [filename(s)]

where f stands for field and n represents the number of the field to extract. The default field delimiter is the tab character.

Example of field
This cut example will extract the second field of the class file and redirect standard output to the file class.lastname:

$ cut -f 2 class > class.lastname
Now seeing the file class.lastname
$ cat class.lastname
Johnson
Smith
Jones
Anderson

Changing the delimitter
We know that default delimitter is tab - what if we want to have a ":",  then we can use -d option.
Eg.:
cut -d: blah blah

More Examples

We will see the usage of cut command by considering the below text file as an example


> cat file.txt
unix or linux os
is unix good os
is linux good os

1. Write a unix/linux cut command to print characters by position?

The cut command can be used to print characters in a line by specifying the position of the characters. To print the characters in a line, use the -c option in cut command

cut -c4 file.txt
x
u
l

The above cut command prints the fourth character in each line of the file. You can print more than one character at a time by specifying the character positions in a comma separated list as shown in the below example

cut -c4,6 file.txt
xo
ui
ln

This command prints the fourth and sixth character in each line.

2.Write a unix/linux cut command to print characters by range?

You can print a range of characters in a line by specifying the start and end position of the characters.

cut -c4-7 file.txt
x or
unix
linu

The above cut command prints the characters from fourth position to the seventh position in each line. To print the first six characters in a line, omit the start position and specify only the end position.

cut -c-6 file.txt
unix o
is uni
is lin

To print the characters from tenth position to the end, specify only the start position and omit the end position.

cut -c10- file.txt
inux os
ood os
good os

If you omit the start and end positions, then the cut command prints the entire line.

cut -c- file.txt

3.Write a unix/linux cut command to print the fields using the delimiter?

You can use the cut command just as awk command to extract the fields in a file using a delimiter. The -d option in cut command can be used to specify the delimiter and -f option is used to specify the field position.

cut -d' ' -f2 file.txt
or
unix
linux

This command prints the second field in each line by treating the space as delimiter. You can print more than one field by specifying the position of the fields in a comma delimited list.

cut -d' ' -f2,3 file.txt
or linux
unix good
linux good

The above command prints the second and third field in each line.

Note: If the delimiter you specified is not exists in the line, then the cut command prints the entire line. To suppress these lines use the -s option in cut command.

4. Write a unix/linux cut command to display range of fields?

You can print a range of fields by specifying the start and end position.

cut -d' ' -f1-3 file.txt

The above command prints the first, second and third fields. To print the first three fields, you can ignore the start position and specify only the end position.

cut -d' ' -f-3 file.txt

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

cut -d' ' -f2- file.txt

5. Write a unix/linux cut command to display the first field from /etc/passwd file?

The /etc/passwd is a delimited file and the delimiter is a colon (:). The cut command to display the first field in /etc/passwd file is

cut -d':' -f1 /etc/passwd

6. The input file contains the below text

> cat filenames.txt
logfile.dat
sum.pl
add_int.sh

Using the cut command extract the portion after the dot.

First reverse the text in each line and then apply the command on it.

rev filenames.txt | cut -d'.' -f1

References

0 comments:

Post a Comment