in reply to Regex and writing lines in file

My understanding is that you want to print the two lines after the match? Then:

use strict; use warnings; open my $fh, "<", "somefile.txt"; while(my $line = <$fh>){ if($line =~ /pattern/){ print "Matched: $line"; print $line if defined( $line = <$fh>); print $line if defined( $line = <$fh>); } } close($fh);