#### One big regex #### ## don't do this and the previous ## read in file my $ptt_file="/g/Viruses/prophage_data/prophage_region.ptt1"; open ($fh, '<', $ppt_file); @lines = <$fh>; close ($fh); $text = join ('', @lines); ## you don't need to precompile this -- it's for clarity ## and in case you ever want to remove these from multiple ## files, i.e., put it in a loop ## Again, I assume the geneid is at the front of the line. my $saved_lines = ''; my $rx_rm_lines = qr/ (^(\w+).+$ [\r\n]) (?{ if (! exists $geneids_to_remove->{$2}) { $saved_lines .= $1; } }) /xm; # the 'm' modifier enables multiline regex ## run the regex (you can use s/.../$1/g if you ## don't want to destroy the string as you search $text =~ s/$rx_rm_lines//g;