#!/usr/bin/perl use strict; use warnings; use File::Spec; my $ordner='source'; my $ausgabe='result'; opendir(my $dh,$ordner) or die("Error open $ordner ($!)\n"); while(my $file=readdir($dh)) { my $path=File::Spec->join($ordner,$file); next unless(-f $path); if(open(my $fh, '<', $path)) { local $/="ENDE\n"; while(my $block=<$fh>) { $block=~s/ENDE\s*$//; next unless($block); my ($name)=$block=~/^(.+?)\s+\d+/; next unless($name); my $out_path=File::Spec->join($ausgabe,$name); if(open(my $ofh, '>', $out_path)) { print $ofh $block; close($ofh); } else { warn("Error open $out_path ($!)\n") } } close($fh); } else { warn("Error open $path ($!)\n"); } } closedir($dh);