open( INPUT, "some.file" ) or die $!; my @content; { local $/; @content = split( /[\r\n+]/, ); } my $tag1 = "whatever"; my $tag2 = "blah"; my $i = 0; while ( $i < $#content ) { # don't test very last line $_ = $content[$i++]; print "$content[$i]\n" if (( /35=8/ and /$tag1/ and /$tag2/ ) or ( /35=9/ and /$tag1/ )); }