0

I have issues with reading csv format like this:

"foo","bar foo, foo bar", foo, bar, far
"bar", "foobar foo, foo" , bar, fobar, bar

Technically, both lines should have 5 fields accordingly with separator ,.

awk -F, '{print NF}' resolver.csv 
6
6

This is where problem goes. AWK treats , as a separator between quotations marks, and provide non accurate results. Giving the separator like -F '","' makes things only worse.

awk -F, '{print $3}' test.csv 
 foo bar"
 foo" 

Any work around?

terdon
  • 242,166
fugitive
  • 1,563
  • 3
    Yes: don't use awk. Use a csv parser. Awk has no knowledge of csv, all it knows is how to split a line on field separators. – terdon Jul 25 '17 at 13:38
  • 1
    e.g. python has a csv module that can parse most CSV formats. I'm pretty sure that perl also has one. – NickD Jul 25 '17 at 13:40
  • 1
    This, also, is far harder than you're expecting. There are a couple of projects I've found to do this, the first is this: https://github.com/geoffroy-aubry/awk-csv-parser – xrobau Jul 25 '17 at 13:43
  • @xrobau thanks for the link. I am currently checking csvtool from Debian repo's, and seems it is doing the job properly.. – fugitive Jul 25 '17 at 13:48

0 Answers0