RE: pie multiline replace

[email protected] (Thomas Bätzler)
Newsgroups perl.beginners
Message-ID <[email protected]>
bill lam <[email protected]> asked:
> 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

That won't work since you ever only see a single line of input.

Since you don't insert new data into the first line of the match,
you could keep track of the previous line in $p and then use
two matches against $p and $_ to determine if you have the line
pair that you want to match.

perl -i -ple '$_ = "cc\n$_" if $p =~ m/aa/ && m/bb/; $p=$_'

HTH,
Thomas
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.