Link to home
Start Free TrialLog in
Avatar of bhoyeto
bhoyeto

asked on

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.


Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

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
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
Avatar of bhoyeto
bhoyeto

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

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
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
There is no easy way to do this in awk. Instead I recommend the use of python with the cvs parsing package.