The cut command is one of the oldest Unix command. That means that it is more then 40 years old. And it shows. It is important to understand that this is a Unix command and behaves in “Unix way”. For example, it uses IFS (Input Field Separators) to determine where to split fields. You can check it with
set | grep IFS. You can also set it, for example, to:
The cut command takes a vertical slice of a file, printing only the specified columns or fields. The columns are selected using the standard IFS or one specified.
cut OPTION [FILE]
Some of the most common options are:
-c, --characters=LIST select only these characters -d, --delimiter=DELIM use DELIM instead of TAB for field delimiter -f, --fields=LIST select only these fields; also print any line that contains no delimiter character, unless the -s option is specified -s, --only-delimited do not print lines not containing delimiters
Let’s do some practical example, having the test.txt file containing:
12345:Administration:james:smith 67891:Staff:stephan:york 18230:Staff:luke:cutwine 62913:Sales:red:blues
Print only character from 1 to 5 of each line.
>cut -c1-5 test.txt 12345 67891 18230 62913
This can be useful when we have fixed width fields, but if we want to print the third column (using as a separator the character :), we can use:
>cut -d: -f3 test.txt james stephan luke red
It’s often the case that you want to use a space as the delimiter. To do so, you must put the delimiter in single quotes, like this:
Finally as a last example we print the contents of the file without the column number two, for this we use the –complement that excludes the column indicated:
cut -d: -f2 --complement test.txt 12345:james:smith 67891:stephan:york 18230:luke:cutwine 62913:red:blues
To print (or exclude) more columns just list them separated by a comma in the -f options.
cut -d: -f2,4 test.txt Administration:smith Staff:york Staff:cutwine Sales:blues
- Reasons why 2015 will be year for Linux on the desktop
- Manage and read your ebooks on Linux with Calibre
- The Best Linux-Based Home Automation Systems for Under $300
Find me on Google+