Sunday, May 22, 2011

[Quick Hint] Get certain columns from a text file

When printing data to a file, formatting it with some delimiter, is often useful to extract certain columns of it. One way of doing this is opening your favorite spreadsheet software and import the file as csv, copy the columns, and save them on another file.


A quick way of doing this in the terminal is using the "cut" command. If we want to extract columns 3 and 5 of file "myFile", with " " (space) as delimiter and output the result to "myOutputFile", we go:

% cat myFile | cut -d" " -f3,5 > myOutputFile

If you want to extract more columns, just separate them using commas.

That's it!

No comments:

Post a Comment