Sysadmin > UtilitIes > SeD > SedReplaceNewline

sed replace newline

sed 's/\n/ /g'
sed 's/$^/ /g'

doesn't work

You have to use

sed ':a;N;$!ba;s/\n/ /g'
for BSD or Mac
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g'

  1. create a label via :a
  2. append the current and next line to the pattern space via N
  3. if we are before the last line, branch to the created label $!ba ($! means not to do it on the last line (as there should be one final newline)).
  4. finally the substitution replaces every newline with a space on the pattern space (which is the whole file).

Link: