my $dir = 'test2'; opendir(DIR,$dir) or die $!; while($datei = readdir(DIR)) { $path = $dir . '/' . $datei; #print "\n$path\n"; next unless -f $path; @entries; { $measure; local $/ = "\nDUT No\n"; open $fh, '<', $path or die $!; while($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; } } } open OUTPUT, '>',$path; print OUTPUT $_,"\n" for @entries; #print $datei; close OUTPUT; }