Re: pie multiline replace

[email protected] (Gunnar Hjalmarsson)
Newsgroups perl.beginners
Message-ID <[email protected]>
bill lam wrote:
> Sorry this must be a faq but I cannot find any answer in google.

Google?? If you think it's a FAQ, you'd better check the Perl FAQ.

     perldoc -q "insert a line"

> Suppose I want to change 2 lines to 3 line in files as follow using perl 
> -p -i -e command
> aa
> bb
> 
> aa
> cc
> bb
> 
> this doesn't work
> perl -p -i -e "s/aa\nbb/aa\ncc\nbb/g;" foo.txt
> 
> what is the correct command (I use linux).

Personally I would prefer a separate Perl script for this task.

     open my $fh, '+<', 'file.txt' or die $!;
     my $data;
     {
         local $/;
         $data = <$fh>;
     }
     $data =~ s/aa\nbb/aa\ncc\nbb/g;
     seek $fh, 0, 0;
     print $fh $data;

More typing, but easier to see what you are doing.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.