On Aug 1, 9:28 am, [EMAIL PROTECTED] wrote:
> I'm trying to substitute all comma separated numbers in a text file
> with the same numbers without commas.  This expression will match the
> numbers: \d{1,3}?(,\d\d\d)+ but how do i refer to each 3 digit block
> after the commas so i can substitute for them?  $1 here just returns
> the last 3 digit number, but i need to substitute for all of them.
> Any help is greatly appreciated.

I guess you want to get rid of commas and put a whitespace instead.
Then simply with:

perl -i.back -pe 's/,/ /g if /\d{1,3}?(,\d\d\d)+/' inputfile

if inputfile contains
234,567,789
123,456,6787,

it will be changed to:
234 567 789
123 456 6787

which is equivalent to:

while (<>) {
    chomp;
    if (/\d{1,3}?(,\d\d\d)+/) {
        s/,/ /g;
        print "$_\n";
    }
}

but the oneliner above does a backup first (in place editing), just in
case.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to