Removing newlines (\n) with sed

Posted on Tuesday, 24th June, 2008 in Life

Today I had to search again on how to remove newline special characters with sed. Thanks to Kamil over at linux.dsplabs.com.au, I found it again rather quickly.

Now, this is just for my own safekeeping, so I don’t end up googling for it again … *shrug*

1
echo -e "Line containing \nnewlines!" | sed ':a;N;$!ba;s/\n//g'

2 Responses to “Removing newlines (\n) with sed”


  1. That’s so ugly though, isn’t it! When I realised it would look like that, I ran looking for another solution. I found one.

    echo -e “Line containing \nnewlines!” | tr -d “\n”


  2. Ah sorry, I see it mentions that one too. Not sure why you’d still want to use sed!

Leave a Reply