my $dir = 'test2'; opendir(DIR,$dir) or die $!; while($datei = readdir(DIR)){ my $path = $dir . '/' . $datei; next unless -f $path; my @entries; { my $measure; local $/ = "\nDUT No\n"; open my $fh, '<', $path or die $!; while( my $entry = <$fh> ){ chomp $entry; @mms = $entry =~ /Measurement:\s*([\d.]+)/; $measure = $mms[0] unless defined $measure; if(grep{ $_ == $measure }@mms){ $entry = "DUT No\n". $entry unless $entry =~ /^DUT No/; push @entries, $entry; } } } print $_,"\n" for @entries; close OUTPUT; }