Linux command: cut (to extract information from text files)

It is very common in data analysis to have to deal with text files containing CSV data or data in columns/rows. Sometimes you just want to look at the third column only, so you try to find a nice little tool to do this job for you, or you open a python session and write your own little program. I used to use awk for that.

awk '{print $3 }' datafile.txt

However, I recently discover cut, which does the same job without. IT works as follows. To extract the column/field 3, type:

cut -f3 datafile.txt

To extract 2 different columns (let us say 1 and 3), and specify the delimiter (here : sign) type:

cut -d: -f1,3 datafile.txt

You can also work with columns instead of fields by using -c instead of -d

Please follow and like us:
This entry was posted in Linux and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published.