Awk Script quote problem

Hi,

How can I make awk treat a double quoted data as one field only?

For example:

Input:
     name, "street, country, zip", age, occupation

Output
    $1 = name
    $2 = street, country, zip
    $3 = age
    $4 = occupation


Thanks.


bhoyetoAsked:
Who is Participating?
 
fridomCEO/ProgrammerCommented:
Then just compress the address. You now that's in $2-$4

Regards
Friedrich
0
 
fridomCEO/ProgrammerCommented:
I don't think you can do it that way if you set FS to , you'll get street etc as different entries. But you could use
name;"street,country,zip";age;occupation if you set FS to ; you'll get name $street etc.

Otherwise if you know that the entries are always three you simply can use them
$1 = name
$2 = street
$3 = couintry
$4 ....

Regards
Friedrich
0
 
PCableGuyCommented:
Hi,
Another option is to remove the commas between street, country, and zip; remove the double
quotes too.

Input:
    name, street country zip, age, occupation

Output:
    $1 = name
    $2 = street country zip
    $3 = age
    $4 = occupation

Good luck
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
bhoyetoAuthor Commented:
Hi,

Thanks for answering my question but those were the clients data and we 're not allowed to change anything. So, I was thinking if there's a way of setting awk to skip data enclosed double quotes.
0
 
PCableGuyCommented:
Hi,

You can pipe the output of awk to sed, the stream editor, to get rid of the double quotes. In your example, the final output would be six fields seperated by spaces.

awk -F"," '{print $1 $2 $3 $4 $5 $6}' yourdata | sed 's/"//g'

As far as awk treating the double quotes as a single field, I don't know if there's a simple solution for that.

0
 
mish33Commented:
You can't make awk treat a double quoted data as one field only.

However you can do
    name = $1
    address = "$2,$3,$4"
    age = $5
    occupation = $6
0
 
mish33Commented:
You can't make awk treat a double quoted data as one field only.

However you can do
    name = $1
    address = "$2,$3,$4"
    age = $5
    occupation = $6
0
 
Raymond_WCommented:
There is no easy way to do this in awk. Instead I recommend the use of python with the cvs parsing package.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.