[bonsai@mainserver tests]$cat spacesnew hello world this is jan m00 hah hehehe one space two spaces three spaces foour spaces test void blah test [bonsai@mainserver tests]$ [bonsai@mainserver tests]$sed -e ':loop;N;$!bloop;${s/\n\([^ ]\)/\1/g;s/\n\( \{1,3\}[^ ]\)/\1/g;}' spacesnew helloworld this isjanm00 hah hehehe one space two spaces three spaces foour spacestestvoidblah test [bonsai@mainserver tests]$