use strict; use warnings; use Regexp::Assemble; my $id_regex = Regexp::Assemble->new; my $IDFILE = ...; # Your filenames here open my $ifh, '<', $IDFILE or die "Could not open $IDFILE: $!"; while ( my $line = <$ifh> ) { $id_regex->add( substr $line, 0, 6 ); } close $ifh; open my $afh, '<', $AMPFILE or die "Could not open $AMPFILE: $!"; open my $ofh, '>', $OUTFILE or die "Could not open $OUTFILE: $!"; while ( my $line = <$afh> ) { chomp $line; if ( $line =~ $id_regex ) { my $next = <$afh>; print $ofh $line, "\t", $next if $next =~ /^Amp-commit/; } } close $ofh; close $afh;