Changing awk delimiter and processing CSV files

One of the functions of awk is to split line of data by white spaces and extract only the desired parts. As such this tool is very suitable for handling comma separated values (CSV) files. The only problem is that CSV files use various delimiters - commas, semi columns, pipes and so on. So to make awk split by our desired delimiter, we just use the -F option:

awk -F, - split by comma
awk -F\; - split by semi column
awk -F\| - split by pipe and so on

The snippet bellow searches for the word test in the second column of pipe delimited CSV file

cat file.csv | awk -F\| '{print $2}' | grep test

 

 

Comments:

alphaniner (25-04-2012 06:15) :
Don't pipe cat to awk, run awk directly on the file:

awk -F\| '{print $2}' file.csv

The same is true of grep, sed, and other commands: they can read files just fine without the help of cat.

CrazyCoder (09-06-2013 15:53) :
Thank you.
This article was really very helpful.
Cheers to you. :)

Back to articles list

This page was last modified on 2024-04-24 00:12:38