# To point out possible problems use strict; use warnings; my $file = 'ip.txt'; my $file_host = 'ip_host.txt'; my @ip; open my $log, '<', $file or die "Can't open file $file: $!"; chomp(@ip = <$log>); close $log; open my $ip_host, '<', $file_host or die "Can't open file $file_host: $!"; foreach my $host (<$ip_host>) { print "$_\n" foreach grep /^\Q$host\E$/, @ip; } close $ip_host;