brucezepplin brucezepplin - 7 months ago 15
Perl Question

remove only *some* fullstops from a csv file

If I have lines like the following:

1,987372,987372,C,T,.,.,.,.,.,.,.,.,1,D,.,.,.,.,.,.,.,1.293,12.23,0.989,0.973,D,.,.,.,.,0.253,0,4.08,0.917,1.048,1.000,1.000,12.998
1,987393,987393,C,T,.,.,.,.,.,.,.,.,1,D,.,.,.,.,.,.,0.152,1.980,16.09,0.999,0.982,D,-0.493,T,0.335,T,0.696,0,5.06,0.871,0.935,0.998,0.997,16.252


how can I replace all instances of
,.,
with
,?,


I want to preserve actual decimal places in the numbers so I can't just do

sed 's/./?/g' file


however when doing:

sed 's/,.,/,?,/g' file


this only appears to work in some cases. i.e. there are still instances of
,.,
hanging around.

anyone have any pointers?

Thanks

Answer

This should work :

sed ':a;s/,\.,/,?,/g;ta' file

With successive ,., strings, after a substitution succeeded, next character to be processed will be the following . that doesn't match the pattern, so with you need a second pass.

:a is a label for upcoming loop

,\., will match dot between commas. Note that the dot must be escaped because . is for matching any character (,a, would match with ,.,).

g is for general substitution

ta tests previous substitution and if it succeeded, loops to :a label for remaining substitutions.

Comments