#!/usr/bin/perl -l use strict; use warnings; my @fileone = qw/123456 123457 123458/; my @filetwo = ("123456 foo", "123456 bar", "123457 foobar", "123455 this wouldn't be printed to new file" ); foreach my $pattern (@fileone) { foreach my $line (@filetwo) { if ($line =~ /^$pattern/) { print $line; } } }