use strict; use warnings; # use these two always! my $pattern= qr/^(Line '(\d+)')$/; # This is the line you're searching for my @holdspace; while () { #read the input if (my $hit= /$pattern/o .. not /$pattern/o) { # this will be executed for every Pattern-line # and the line after that. if ($hit==1) { # If it was the first: Clear the holdspace @holdspace= (); } elsif ($hit=~ /e0/i) { # If it's the last print everything print "NewLine1\n",@holdspace,$_; next; } # Store the pattern lines push @holdspace, "(+$2) $1;fac=$2\n"; next; } # Print everything else print; } # That's it print "MeanLine\n"; __END__ Line1 Line2 Line3 Line '1' Line '2' Line '3' Line4 Line5