Q: Howto enclose comma separated words with " 
Author Message
 Q: Howto enclose comma separated words with "

Hi,

Apologies if this question has been asked before, perhaps someone can point
me
to FAQ if this is the case...

item1,item2

is there a way to enclose each comma separated item with a "  i.e

"item1","item2"

Thanks for your help...

Nick



Sat, 17 Aug 2002 03:00:00 GMT  
 Q: Howto enclose comma separated words with "

Quote:

> Hi,

> Apologies if this question has been asked before, perhaps someone can point
> me
> to FAQ if this is the case...

> item1,item2

> is there a way to enclose each comma separated item with a "  i.e

> "item1","item2"

> Thanks for your help...

> Nick


if you have only comma separated data, this will do

gawk -F, 'BEGIN{OFS=","}{ for(i=1;i<=NF;i++)$i="\""$i"\"" ;print}'
item1,item2
"item1","item2"

--
__________   _____________________________________________________
          \_/                     Eiso AB



Sat, 17 Aug 2002 03:00:00 GMT  
 Q: Howto enclose comma separated words with "

Hi Eiso !...

Many, many, many thanks... that worked great !...
I owe you a beer for that !.

Kind Regards

Nick


Quote:

> > Hi,

> > Apologies if this question has been asked before, perhaps someone can
point
> > me
> > to FAQ if this is the case...

> > item1,item2

> > is there a way to enclose each comma separated item with a "  i.e

> > "item1","item2"

> > Thanks for your help...

> > Nick

> if you have only comma separated data, this will do

> gawk -F, 'BEGIN{OFS=","}{ for(i=1;i<=NF;i++)$i="\""$i"\"" ;print}'
> item1,item2
> "item1","item2"

> --
> __________   _____________________________________________________
>           \_/                     Eiso AB



Sat, 17 Aug 2002 03:00:00 GMT  
 Q: Howto enclose comma separated words with "

Quote:

>Hi,

>Apologies if this question has been asked before, perhaps someone
>can point me to FAQ if this is the case...

>item1,item2

>is there a way to enclose each comma separated item with a "  i.e

>"item1","item2"

awk '{gsub(/[^,][^,]*/,"\"&\"");print}' infile > outfile

this assumes the items will have at least one character, and that
your version of awk supports gsub.

with sed one could do something like:

sed -e 's/[^,][^,]*/"&"/g' infile > outfile

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sat, 17 Aug 2002 03:00:00 GMT  
 Q: Howto enclose comma separated words with "

Quote:



..
>>is there a way to enclose each comma separated item with
>>a "  i.e  "item1","item2"

>awk '{gsub(/[^,][^,]*/,"\"&\"");print}' infile > outfile

>this assumes the items will have at least one character,

..

Picky - why not handle zero length fields too?

awk '{gsub(/,/, "\",\""); print "\"" $0 "\""}' in > out

* Sent from AltaVista http://www.altavista.com Where you can also find related Web Pages, Images, Audios, Videos, News, and Shopping.  Smart is Beautiful



Sat, 17 Aug 2002 03:00:00 GMT  
 Q: Howto enclose comma separated words with "


Quote:




>..
>>>is there a way to enclose each comma separated item with
>>>a "  i.e  "item1","item2"

>>awk '{gsub(/[^,][^,]*/,"\"&\"");print}' infile > outfile

>>this assumes the items will have at least one character,
>..

>Picky - why not handle zero length fields too?

>awk '{gsub(/,/, "\",\""); print "\"" $0 "\""}' in > out

No reason, except that the original poster did ask for "words"
separated by commas.  I was merely pointing out the limitation
of my implementation(s).

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sat, 17 Aug 2002 03:00:00 GMT  
 Q: Howto enclose comma separated words with "

   >Hi,
   >Apologies if this question has been asked before, perhaps someone
   >can point me
   >to FAQ if this is the case...
   >item1,item2
   >is there a way to enclose each comma separated item with a "  i.e
   >"item1","item2"
   >Thanks for your help...
   >Nick
{sub(/^/,"\"");gsub(/,/"\",\"");sub(/$/,"\"")}

Net-Tamer V 1.08X - Test Drive



Sat, 17 Aug 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Howto parse text enclosed by parens

2. parsing comma separated field

3. Comma Separated Values

4. formatter for comma separated files

5. Comma Separated File

6. extracting comma-separated substrings

7. Q: going from tuple to comma-separated list?

8. Formatting: separating commas for non-decimals

9. CSV-2.0 Comma separated value parser

10. HELP with the "system" comma

11. "Separate x and y values."

12. Use of "is separate" and compilatio

 

 
Powered by phpBB® Forum Software