(How) can I remove all newlines (\n) using sed?

Viewed 37613

I tried to remove all newlines in a pipe like this:

(echo foo; echo bar) | sed -e :a -e N -e '$!ba' -e 's/\n/ /g' | hexdump -C

Which results on debian squeeze in:

00000000  66 6f 6f 20 62 61 72 0a                           |foo bar.|
00000008

Not removing the trailing newline.

tr -d '\n' as in How do I remove newlines from a text file? works just fine but isn't sed.

8 Answers

you need to replace \n with something else, instead of removing it. and because lines are seperated by \n (at least on GNU/Linux) you need to tell sed to look for some other EOL character using -z, like so:

> echo -e "remove\nnew\nline\ncharacter" | sed -z "s/\n//g"
removenewlinecharacter> 

From sed --help

  -z, --null-data
                 separate lines by NUL characters

sed would normally remove entire lines (using /d), like so:

> echo -e "remove\nnew\nline\ncharacter" | sed "/rem\|char/d"
new
line
> echo -e "remove\nnew\nline\ncharacter" | sed -r "/rem|char/d"
new
line
>

using /d every line containing an EOL would be deleted, which are all lines. (one)

> echo -e "remove\nnew\nline\ncharacter" | sed -z "/\n/d"
> 

HTH

The answer of Stefan Kaerst is perfect:

(echo foo; echo bar) | sed -z "s/\n//g" 

This will export "foobar", as the parameter -z, --null-data separates end of lines by NUL characters (even thought there are no nulls in the text: foobar="66 6f 6f 62 61 72").

An useful example

I use this command to restore paragraphs with broken lines:

cat novel.txt | sed -r 's/^(.{53,80})$/\1<br>/;' | sed -z "s/<br>\n//g" 

This joints long lines if they are longer then 53 characters.

A) Find lines longer then 53 characters and add '<br>' at their end.

B) Find '<br>' with newline and remove it.

Thank you, Mr. Kaerst!

Related