1752806 Members
6504 Online
108789 Solutions
New Discussion

sed string replacement

 
SOLVED
Go to solution
Camel_1
Valued Contributor

sed string replacement

Hi there,

Suppose a text file contains records such as:
12345,"47,835.33",abc,…

How can I remove the optional double-quote enclosure and thousand separator so as to transform it to:
12345,47835.33,abc,…

Thanks,

Simon
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: sed string replacement

Hi Simon:

Try:

# perl -ple 's/"(\d+),(\d+\.)(\d+)"/\1\2\3/;s/"//g' filename

Regards!

...JRF...
Patrice Le Guyader
Respected Contributor

Re: sed string replacement

Demat Simon,

I assume that your filename is toto :

sed -e 's/\"//g' -e 's/\.//g' toto

Hope this helps
Kenavo
Pat
Good judgement comes with experience. Unfortunately, the experience usually comes from bad judgement.
Peter Nikitka
Honored Contributor

Re: sed string replacement

Hi,

my solution is in awk:

awk -F, -v quo='"' '/"[0-9,.]*"/ {startq=0;str="";for (i=1;i<=NF;i++) {if($i ~ quo) {startq=!startq;
sub(quo,"",$i)}
str=str""$i;if(!startq && (iprint str;next}
{print}' TEXTFILE

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Camel_1
Valued Contributor

Re: sed string replacement

thanks