Using awk to Work with CSV files in the Command Line Terminal

Written by
Date: 2012-04-02 17:15:00 00:00


I work a lot on the terminal, either on my Linux or Mac computers, and when I need to work with CSV files, awk is a very good ally.

Let's say you have a file like this one:

Linux, Mac OS X, Windows XP, Windows 8
Ubuntu, iOS, Windows Mobile, Blackberry OS
Firefox, Safari, Internet Explorer, Blackberry Browser

And let's say you want to print only column 2 of that file, here is how:

awk -F "," '{print $2 }' filename.csv

And if you want to print two columns at once:

awk -F "," '{print $1, $3 }' filename.csv

Column 1 and 3 in this case.

If you separator is not a command, and it is let's say a pipe, just do something like this:

awk -F "|" '{print $1, $3 }' filename.csv