in reply to Find & Replace

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

Replies are listed 'Best First'.
Re^2: Find & Replace
by mantra2006 (Hermit) on Aug 10, 2006 at 14:58 UTC
    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;
        Thanks alot for the replies

        I got the solution


        Sridhar
        Hello The script is working fine thanks for the script. Any idea
        if the file is big how to achieve this task


        Thanks in advance Sridhar
        Hello


        If the file is big is there way to do this task...thanks again for your help
        Sridhar