while ( my $line = ) { chomp $line; # remove the spaces from start of line if ( ! ( $line =~ s/^\s+// ) ) { # print newline (not for first line) print "\n" if $. != 1; } print $line; }