my $id_file = 'id.txt'; my %id; open my $ID, "<", $id_file or die "Error opening $id_file $!"; while (<$ID>) { chomp; $id{$_} = 1; } close $ID; while (<>) { # assumes file 2 is passed to the script - to be adjusted to real conditions my $key = $1 if /^(\w+)/; print if exists $id{$key}; }