?
Solved

Awk Script quote problem

Posted on 2007-10-12
10
Medium Priority
?
717 Views
Last Modified: 2013-12-26
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.


0
Comment
Question by:bhoyeto
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 20064174
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
 
LVL 12

Expert Comment

by:PCableGuy
ID: 20069756
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
 

Author Comment

by:bhoyeto
ID: 20070904
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Accepted Solution

by:
fridom earned 500 total points
ID: 20070981
Then just compress the address. You now that's in $2-$4

Regards
Friedrich
0
 
LVL 12

Expert Comment

by:PCableGuy
ID: 20071609
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
 
LVL 15

Expert Comment

by:mish33
ID: 20075360
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
 
LVL 15

Expert Comment

by:mish33
ID: 20075361
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
 

Expert Comment

by:Raymond_W
ID: 20102443
There is no easy way to do this in awk. Instead I recommend the use of python with the cvs parsing package.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month15 days, 5 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question