mantra2006 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks I need to find commas (,) in a file and remove it...my file
structure will be as follows

Hello,World,23,,,,
test,test,test
test,test,test
test,test,test
test,test,test
end,bye,,,,

My script has to remove commas after 23, that means after
23 there were 4 commas it has to remove 3 and also in the
footer after bye, 3 commas have to be removed...

Is there a best way to achieve this task...

thanks in advance

Sridhar

Replies are listed 'Best First'.
Re: Find & Replace
by davorg (Chancellor) on Aug 10, 2006 at 14:51 UTC
    perl -i~ -pe 's/,+/,/g' your_filename_goes_here
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Hello

      This command worked..is there any way to specify the
      line numbers so that only first line and last line of the
      file will be replaced
      Thanks & Regards

      Sridhar
        perl -i -pe '(1==$. or eof) && s/,,+/,/' file_name


Re: Find & Replace
by McDarren (Abbot) on Aug 10, 2006 at 14:50 UTC
    So for each line in your file, you want to replace one or more commas at the end of the line, with a single comma - yes?

    If that's the case, it's as simple as:

    while (<LINES>) { $_ =~ s/,+$/,/; }

    (You could actually do this from the command line with something like: perl -pi -e 's/,+$/,/' file.txt)

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Find & Replace
by prasadbabu (Prior) on Aug 10, 2006 at 14:52 UTC

    Sridhar, Here is one way to do it.

    use strict; use warnings; my $string = 'Hello,World,23,,,, test,test,test test,test,test test,test,test test,test,test end,bye,,,,'; $string =~ s/((?:first line last word|last line last word),)[^\n]*/$1/ +g; #in your file $string =~ s/((?:23|bye),)[^\n]*/$1/g; #here in this string print $string;

    update:Oops! Already two answers above me. Better solutions from davorg++ and McDarren++

    Prasad

      Hello

      Thanks for the replies.

      The content will be in file and I need to replace commas in first and last line only...

      Thanks & Regards

      Sridhar
        Well, if the file is not too big, a simple (but slightly convoluted) way to do that would be to read all the lines into an array, edit the first and last elements of the array, and then write the file back out again. Like so:
        open IN, "<", "file.txt" or die "Cannot open file.txt for reading:$!\n +"; my @lines = <IN>; $lines[0] =~ s/,+$/,/; $lines[-1] =~ s/,+$/,/; open OUT, ">", "file.txt" or die "Cannot open file.txt for writing:$!\ +n"; print OUT for @lines; close OUT;
Re: Find & Replace
by ercparker (Hermit) on Aug 10, 2006 at 18:59 UTC
    you can also try this:
    while (<DATA>) { s{(?:(?<=23,)|(?<=bye,)),+}{}; print; } __DATA__ Hello,World,23,,,, test,test,test test,test,test test,test,test test,test,test end,bye,,,,
      Moving the ,+ outside of the alternation also works.

      while (<DATA>) { s{(?:(?<=23,)|(?<=bye,)),+}{}; print; }

      Cheers,

      JohnGG