Operating System - HP-UX
1829012 Members
2216 Online
109986 Solutions
New Discussion

Putting Double Quotes in a text file

 
SOLVED
Go to solution
Kurtkarl
Frequent Advisor

Putting Double Quotes in a text file

Hi,

I'm not sure if this was asked before But I tried searching the forum and could'nt find one. Anyways, I have a txt file and wanted to put a double quotes on every numbers within the column. Example

THis is the original file
201,303,412,484,576
202,306,413,485,578

and I want it to be as this one
"201","303","412","484","576"
"202","306","413","485","578"

Your help would be greatly appreciated
THanks
Joey
Just starting to learn thru this forum
9 REPLIES 9
Rodney Hills
Honored Contributor
Solution

Re: Putting Double Quotes in a text file

You can use sed

sed -e 's/,/","/;s/^/"/;s/$/"/' outfile

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: Putting Double Quotes in a text file

oops- I forgot the "g" option on the first "s" command.

sed -e 's/,/","/g;s/^/"/;s/$/"/' outfile

-- Rod Hills
There be dragons...
harry d brown jr
Honored Contributor

Re: Putting Double Quotes in a text file

cat infile | sed "s/\([0-9]\{3\}\)/\"\1\"/g" > outfile


live free or die
harry
Live Free or Die
Roger Baptiste
Honored Contributor

Re: Putting Double Quotes in a text file

hi,

Same thing can be done via vi editor.
In escape mode:
%s/,/","/g
%s/^/"/
%s/$/"/

HTH
raj
Take it easy.
harry d brown jr
Honored Contributor

Re: Putting Double Quotes in a text file

If your numbers vary, as in


201,303,888412,484,576
101,403,712,884,9999876,3,44

then this will work:

cat infile | sed "s/\([0-9]\{1,\}\)/\"\1\"/g"


producing this:

"201","303","888412","484","576"
"101","403","712","884","9999876","3","44"


live free or die
harry
Live Free or Die
SHABU KHAN
Trusted Contributor

Re: Putting Double Quotes in a text file

Hi Joey,

Here is another way with sed:

This will prefix and suffix any number of numeric characters with double quotes:

sed 's/[0-9]*/"&"/g' filename


Thanks,
Shabu
Rodney Hills
Honored Contributor

Re: Putting Double Quotes in a text file

Shabu,

Use of "*" matches null to, so you need to modify your sed with-

sed 's/[0-9]\{1,\}/"&"/g'

-- Rod Hills
There be dragons...
H.Merijn Brand (procura
Honored Contributor

Re: Putting Double Quotes in a text file

Without using a second file, altering the file itself in one sweep:

perl -pi -e 's/\d+/"$&"/g' file
Enjoy, Have FUN! H.Merijn
SHABU KHAN
Trusted Contributor

Re: Putting Double Quotes in a text file


Thanks Rodney for pointing it out ...

Joey,
This will also match blanks as mentioned by Rodney "*" matches null also... Thanks for the points.

N/A for this please ..

-Shabu