echo -e " fu bar\n foo \n\n bar \n\n\n baz " | perl -nE 's/^\s+//; s/\s+$/ /; $new .= $_; END{ $new =~ s/\s+$//; say ">$new<"; }'