Check perlvar for more info on $/.$/ = ""; # input record separator set to empty string my $last_rec; while (<>) { # each record is terminated by /\n{2,}/ if ( /^def/ ) { print $last_rec.$_ if ( $last_rec ); } else { $last_rec = $_; } }
UPDATE: Sorry, I probably misunderstood the problem. If blank lines are actually distributed as shown in the OP, then something like this would be needed in order to use "paragraph-based" input:
That's pretty icky, really -- very sorry. I suggest you just slurp the whole file and use one of the regex solutions from an earlier reply.$/ = ""; my $last_prefix = my $last_target = ''; while (<>) { if ( /^abc/ ) { print $last_prefix.$last_target if ( $last_target ); $last_prefix = $_; $last_target = ''; } elsif ( /^def/ ) { $last_target = $_; } elsif ( $last_target ) { $last_target .= $_; } elsif ( $last_prefix ) { $last_prefix .= $_; } }
Another update: I still did not get the OP's intent -- but no point fixing this code, since other solutions are up there (I think). Never mind me.
In reply to Re: How do I match the closest of repeated strings?
by graff
in thread How do I match the closest of repeated strings?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |