brucezepplin brucezepplin - 2 years ago 87
Perl Question

remove only *some* fullstops from a csv file

If I have lines like the following:


how can I replace all instances of

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?


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download