for my $file (@files) { open FH, '<', $file or die "$!\n"; while ( ) { next if $_ eq 'foo'; # Where foo can only appear once in the file print; } close FH }