perl -00 -pe 's/ $/\012/ if s/\n+/ /g and eof' file