#!/usr/bin/perl use strict; use warnings; my $prev = ""; my $done = 0; while () { # sliding two-line buffer $_ = $prev.$_; # get rid of middle newline, so it doesn't interfere with match s/\n//; s/<\?string\?>//g; # split buf # first part will be output, second part kept # for joining with next input line ($_, $prev) = unpack "a".length($prev)."a*", $_; print "$_\n" if 2 .. $done; # corner case: have last line be printed as well if (eof and !$done++) { $_ = ""; redo } } __DATA__ hello there this is a bad that we need to take away. Here is another bad that needs to go too. #### hello there this is a bad that we need to take away. Here is another bad that needs to go too.