awk combine 2 commands for csv file formatting

63 Views Asked by At

I have a CSV file which has 4 columns. I want to first:

  1. print the first 10 items of each column
  2. only print the items in the third column

My method is to pipe the first awk command into another but i didnt get exactly what i wanted:

awk 'NR < 10' my_file.csv | awk '{ print $3 }'
2

There are 2 best solutions below

1
On

The only missing thing was the -F.

awk -F "," 'NR < 10' my_file.csv | awk -F "," '{ print $3 }'
0
On

You don't need to run awk twice.

awk -F, 'NR<=10{print $3}'

This prints the third field for every line whose record number (line) is less than or equal to 10.

Note that < is different from <=. The former matches records one through nine, the latter matches records one through ten. If you need ten records, use the latter.

Note that this will walk through your entire file, so if you want to optimize your performance:

awk -F, '{print $3} NR>10{exit}'

This will print the third column. Then if the record number is greater than 10, it will exit. This does not step through your entire file.

Note also that awk's "CSV" matching is very simple; awk does not understand quoted fields, so the record:

red,"orange,yellow",green

has four fields, two of which have double quotes in them. YMMV depending on your input.