Monday, August 31, 2009

The Linux cut command

cut is one of the lesser-known commands in the same category of tools such as grep, awk and sed. Nevertheless, I find it more convenient than the other tools in some circumstances. Let's say I have the following lines with ":" as the field delimiter:
abc:12:DEF
ghi:3a4:JKLM
opqr:56b:STUV


To print out the values in the second column, I can use the command

cut -d: -f2

where -d: specifies that the field delimiter is ":", -f2 specifes that the field to print is 2.

If the delimiter is the space character, the command will become

cut -d\ -f2

Notice there are 2 space characters after the backslash. The backslash will escape the space character that follows it.