while ( my $line = ) { chomp $line; # remove the spaces from start of line if ( ! ( $line =~ s/^\s+// ) ) { print "\n" if $. != 1; } # remove spaces from end of line $line =~ s/\s+$//; print $line; } __DATA__ firstline54324563213612531436436463246324634 secondlidfndsfdsfdsfd thirdlinedsfsdf firstline54324563213612531436436463246324634 secondlidfndsfdsfdsfd thirdlinedsfsdf #### $ perl guess.pl firstline54324563213612531436436463246324634secondlidfndsfdsfdsfdthirdlinedsfsdf firstline54324563213612531436436463246324634secondlidfndsfdsfdsfdthirdlinedsfsdf