use strict; use warnings; my $found_body = 0; my $filename = shift or die "$0 FILENAME\n"; open FILE, '<', $filename or die "$!\n"; while (my $line = ) { chomp $line; if ($found_body == 1) { $line =~ s/\s+$//; # right trim $line =~ s/\s+/ /g; # remove all repeating whitespace if (eof(FILE)) { if (length($line) > 0) { # suppress last line if empty print "$line\n"; } } else { print "$line\n"; } } # Check if the line is empty (contains only whitespace) if ($line =~ /^\s*$/) { $found_body = 1; } } close FILE