Wednesday, June 12, 2013

Cut command reference

Cut is a simpler alternative to the sed command. Cut is also very efficient and flexible..

The examples specified below is from Solaris 11.1 and  RedHat Enterprise Linux 6.4, however most of the Linux / Unix distributions supports the cut command.

We shall use the below file for our examples.

root@sol1:~/jk# cat testfile.txt
This is a test file to demonstrate "cut" command.
Cut can be used for line filtering.
Cut is simpler than awk and sed commands.
root@sol1:~/jk#


1. Select a field from a file.
You can combine -f and -d to print required field. -f specifies the field and -d specified the delimiter to be used to find the filed.

root@sol1:~/jk# cut -d':' -f1 /etc/passwd | head -5
root
daemon
bin
sys
adm
root@sol1:~/jk#


2. Select multiple fields from a file.
To select multiple fields use comma separated numbers with -f.

root@sol1:~/jk# cut -d' ' -f 1,5 testfile.txt
This file
Cut for
Cut awk
root@sol1:~/jk#
Here 'space' is the delimiter.


3. Select Column of characters.
To print selected column or character from a file use -c option.

root@sol1:~/jk# cut -c2 testfile.txt
h
u
u
root@sol1:~/jk#
 
As you can see, h,u,u are the 2nd character on each line.


4.  Select column of characters using range.
 
To print from 3rd character to the end :

root@sol1:~/jk# cut -c3- testfile.txt
is is a test file to demonstrate "cut" command.
t can be used for line filtering.
t is simpler than awk and sed commands.
root@sol1:~/jk#

To print till 5th character :

root@sol1:~/jk# cut -c-5 testfile.txt
This
Cut c
Cut i
root@sol1:~/jk#

To print from 5th character to 10th character.
 
root@sol1:~/jk# cut -c5-10 testfile.txt
 is a
can be
is sim
root@sol1:~/jk#

Whole lines will be print if no number is specified before or after '-'.

root@sol1:~/jk# cut -c- testfile.txt
This is a test file to demonstrate "cut" command.
Cut can be used for line filtering.
Cut is simpler than awk and sed commands.
root@sol1:~/jk#

5. Print fields only when the delimiter specified is present.

If the delimiter specified is not present in the file, cut will print the whole lines.

root@sol1:~/jk# cut -d'|' -f1 /etc/passwd | head -5
root:x:0:0:Super-User:/root:/usr/bin/bash
daemon:x:1:1::/:
bin:x:2:2::/usr/bin:
sys:x:3:3::/:
adm:x:4:4:Admin:/var/adm:
root@sol1:~/jk#

Use '-s' to prevent this behaviour.

root@sol1:~/jk# cut -d'|' -s -f1 /etc/passwd | head -5
root@sol1:~/jk#

6. To select all fields except the ones specified.
Use --complement to invert cut's behavior. Note '--complement' is not available in Solaris.

root@sol1:~/jk# cut -d' ' --complement -f1,3 testfile.txt
is test file to demonstrate "cut" command.
can used for line filtering.
is than awk and sed commands.
root@sol1:~/jk#


7. Change output delimiter for ease of reading.
You can use '--output-delimiter' to custom specify the delimiter to print. Note : --output-delimiter is not available in Solaris.

root@rhel:~/jk# grep "/bin/bash" /etc/passwd | cut -d':'  -s -f1,6,7 --output-delimiter='#'
root#/root#/usr/bin/bash
root@rhel:~/jk#

You can also use the escape characters as follows. For changing the delimiter to "new line":

root@sol1:~/jk# grep "/bin/bash" /etc/passwd | cut -d':'  -s -f1,6,7 --output-delimiter=$'\n'
root
/root
/usr/bin/bash
root@sol1:~/jk#

Remember to use "$" as the escape character.

Cut is a very cool and simple utility. Master it !!

JK.