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.
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.
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
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
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 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
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
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.
name;"street,country,zip";
Otherwise if you know that the entries are always three you simply can use them
$1 = name
$2 = street
$3 = couintry
$4 ....
Regards
Friedrich