Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

marked as duplicate by Gilles, rahmu, Thor, uther, jasonwryan Feb 1 '13 at 2:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

up vote 1 down vote accepted

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]'
share|improve this answer
    
This is exactly what I needed. Thanks. –  toobsco42 Jan 31 '13 at 20:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.