Using AWK on CSV Files

You can use AWK to quickly look at a column of data in a CSV file.

In my case, the CSV files are in the following format:

"field1","field2","field3"

To view the 3rd field of every line, you can use the following command.

awk -F "\"*,\"*" '{print $3}' file.csv

As another example, take the following pipe delimited format:

field1|field2|field3

To view the 2nd field of every line, you can use the following command.

awk -F "|" '{print $2}' coldwell.csv

You can also pull multiple columns with one command. The following example pulls the 3rd column and then the 1st column.

awk -F "\"*,\"*" '{print $3,$1}' AvailableInventory.csv

If you separate the arguments with a comma (as in the example above) they will be concatenated with space between the items. You can also use a space (as in the example below) and the items will have no space between them.

awk -F "\"*,\"*" '{print $3 $1}' AvailableInventory.csv

If you wanted to add a separator between those columns, you can add some text in quotes and it will be output as-is. In the example below, I'll add a pipe (|) character between the two columns.

awk -F "\"*,\"*" '{print $3 "|" $1}' AvailableInventory.csv

comments powered by Disqus
using_awk_on_csv_files.txt · Last modified: 2013/03/14 16:52 by Joel Dare