# (assign $infile, $outfile as above, open IN, OUT as in your post, then: while () { print OUT unless ( /774(0[346789]|1[123468])/ ); } # but if the set of numerics to exclude changes regularly, # you'll want to figure out how to convey the proper set # to the script on start-up, and construct a regex along the # lines shown in your original code.