Replace all newlines to space except the last
You can use paste -s -d ' ' file.txt
:
$ cat file.txt
one line
another line
third line
fourth line
$ paste -s -d ' ' file.txt
one line another line third line fourth line
You can use tr
to replace all newlines to space and pass the output to sed
and replace the last space back to a newline:
tr '\n' ' ' < afile.txt | sed '$s/ $/\n/'
Re-implementing vonbrand's idea in Perl, provided the file is small enough:
perl -p00e 's/\n(?!\Z)/ /g' your_file