Using AWK to work with CSV files

Datetime:2016-08-23 01:00:35         Topic: AWK          Share        Original >>
Here to See The Original Article!!!

Should you have a CSV file, you may want to convert that into another form.

AWK can help there. Here’s the basic AWK command for CSV files:

awk -v q="'" --field-separator ',' '{print q $1 $2 q}'

We’re saying, be verbose -v , use ' as the variable q (sometimes this is useful) and separate the fields using , .

Then the work in {} is where is all happens. In this case we’re using print to print.

We’re printing first and the second field with no spaces inbetween (either a blank place in double quotation marks or a comma will give a space). We also use q to add a single quotation mark.

For example, given this CSV data in sample.csv:

chris,buckly,ethereal spirit

This awk command cat sample.csv | awk -v q="'" --field-separator ',' '{print q $3 "=" $1 q}' will output:

'ethereal spirit=chris'

If you use AWK’s print to format a unix command, you can then pipe awk’s output to bash and run that command.


Put your ads here, just $200 per month.