#! /usr/bin/perl use strict; use warnings; my $file = 'test.dat'; my $out = 'test.dat.out'; my $count_ = 1; my $datensatz = ''; { open(my $outfh,">$out") or die $!; open(my $fh,"<$file") or die $!; while(my $entry = <$fh>){ $entry =~ s/\r?\n//g; if($entry =~ /^\s*?#/){ print $outfh $entry,"\n"; } else{ if($entry =~ /^\s*?$/){ $count_++; } else{ $datensatz .= $entry."\n"; } if($count_ % 3 == 0){ $datensatz = getDatensatz($datensatz); print $outfh $datensatz; $count_ = 1; $datensatz = ''; } } } if($datensatz){ $datensatz = getDatensatz($datensatz); print $outfh $datensatz; } close $fh; close $outfh; } sub getDatensatz{ my ($entry) = @_; my $bool = 0; my $set = ''; my @steigend; for my $line(split(/\r?\n/,$entry)){ chomp $line; my $wert = (split(/\s+/,$line))[-1]; $bool = 1 if($bool || $wert =~ /^4/); unless($bool){ push(@steigend,$line); } else{ $set .= $line."\n"; } } $set .= $_."\n" for(@steigend); $set .= "\n\n"; return $set; }