Home > Blockchain >  Working with data in quoted fields with GNU AWK
Working with data in quoted fields with GNU AWK

Time:12-17

My apologies if this has been asked before. I've looked but couldn't find a satisfactory answer.

I have this sample file, test.csv:

"abc","def,apple","2019-10-10"

I'd like to output any of the first fields based on the date in the last one. I've tried this

gawk -vFPAT='[^,]*|"[^"]*"' '($3 >= "2018-10-10") {print $1}' test.csv

but the output is empty.

If I remove the quotes around the date in test.csv, the above works. The command outputs "abc". I have a long file that follows the above pattern and I'm sure I can use a simple regex to remove the quotes around the dates, but is there a way to avoid this by changing the above gawk command?

CodePudding user response:

With awk:

awk -F ',' '$NF >= "\"2018-10-10\""{ print $1 }' test.csv

or

awk -F '"' '$(NF-1) >= "2018-10-10"{ print $2 }' test.csv

$NF contains last column

  • Related