3

Possible Duplicate:
Is there a robust command line tool for processing csv files?

I am trying to parse the second line in a file (which contains commas within double quotes)

"100 Watt A19 10,000 Hour - Clear Light Bulb",American-Light-Bulb-Mfg,552,Indoor Living>Home Furnishings>Lighting>Light Bulbs,"Product Code: H20821 Energy Used: 100 Watts Rated Life: 10,000 Hours Volts: 130 Base: E26 - Medium Screw (standard household base) Length: 4.45 Inches Diameter: 2.375 Inches Bulb Type: A19",1.75,Yes,http://www.buylighting.com/ProductDetails.asp?ProductCode=H20821&click=104182,http://www.buylighting.com/v/vspfiles/photos/H20821-2T.jpg,,New,,,0.1,H20821,,,,,,,,,,,,,,,,.25,Absolute,,,,,,,

head -2 file.csv | tail -1 | awk -F',' '{print $1}'

But it outputs:

"100 Watt A19 10

How can I use awk to get "100 Watt A19 10,000 Hour - Clear Light Bulb" as the value of $1? Also how can I use awk to get "Product Code: H20821 Energy Used: 100 Watts Rated Life: 10,000 Hours Volts: 130 Base: E26 - Medium Screw (standard household base) Length: 4.45 Inches Diameter: 2.375 Inches Bulb Type: A19" as the value of $5?

1 Answers1

2

That's not something awk is terribly good at, try using perl instead:

head -n 2 file.csv |
  tail -n 1 |
  perl -lne '@a = /((?:".*?"|.)*?)(?:,|$)/g; print $a[0]'