# read required ids into an array my @reqids = ; chomp @reqids; # build regex containing all required ids my $re = join '|', map { "\Q$_\E" } @reqids; $re = qr/$re/; # Process infile a line at a time looking for matches while () { if (/$re/) { print "Matched - $_"; if (/SELECT/) { for (1 .. 3) { print scalar ; } } } }