#!/usr/bin/perl use strict; use warnings; my @files = grep { $_ !~ /^~/ } (<*.txt>); my @matches; foreach my $file (@files) { open (my $fh, '<', $file) or die $!; /(NR \d{7})/g and push @matches, $1 while (<$fh>); close $fh or die $!; } print "@matches\n";