Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm so confused with GNU sed, POSIX sed, and BSD sed's myriad of options. How do I replace literal \n with the newline character using these three sed types?

share|improve this question
They have many different things, what part do you want to know? – cuonglm Jul 4 '14 at 13:25
@Gnouc i think now it's clear. – Avinash Raj Jul 4 '14 at 13:27
Did you have a look at this question ? – groxxda Jul 4 '14 at 13:29
What do you want to happen on input like foo\\nbar or foo\\\nbar? – Gilles Jul 5 '14 at 10:44
simple, foo\\ in the next line it would be bar. – Avinash Raj Jul 5 '14 at 10:47
sed 's/\\n/\

Notice the backslash just before hitting return in the replacement string.

share|improve this answer
does this works for all kind of sed's? – Avinash Raj Jul 4 '14 at 13:44
I believe it should; I tested only on Linux using gnu version. – unxnut Jul 4 '14 at 14:07
@AvinashRaj. That's POSIX and worked with the original sed command in Unix v7 in 1979. The only place where that might not work would be non-POSIX stripped-down implementations of sed like some stripped-down busybox-based ones. That won't work in csh but it's a csh issue. – Stéphane Chazelas Jul 4 '14 at 14:13

Since you already have your portable sed answer, I'll just mention that sed is rarely the best tool if you need to somehow manipulate newlines. Perl is very nearly as portable as sed, is installed by default on most *nix systems and can deal with this very easily:

printf '%s\n' 'aa\nbb' | perl -pe 's/\\n/\n/g'

Another very portable choice is awk:

printf '%s\n' 'aa\nbb' | awk  '{gsub("\\\\n","\n")};1'

On Solaris, remember to use the standard awk in /usr/xpg4/bin, the one in /bin is an historical one and should not be used for new scripts.

share|improve this answer
I'd say awk has similar portability issues (Is my awk plain old awk, nawk or gawk?). Actually, the version in my Solaris boxes lacks gsub(). My vote here is for Perl (perl -nlawe '...' approximates the automatic behaviour of awk). – arielCo Jul 4 '14 at 22:07
@arielCo: For awk portability, stick with a version of awk that is POSIX compliant and only use those features and you should be fine. On Solaris you will find one as /usr/xpg4/bin/awk . – Scrutinizer Jul 5 '14 at 4:48
@terdon how about this echo "aa\nbb" | awk '{gsub(/\\n/,"\n");}1;'? – Avinash Raj Jul 5 '14 at 12:05
@arielCo actually, awk is more portable than perl. My second awk approach which does not use gsub() should work just about everywhere. Even busybox has an awk. – terdon Jul 5 '14 at 12:07
gsub() is POSIX (gsub, was added in SVR3.1, POSIX awk is based on SVR4 with a few additions see the gawk manual for more information), gensub() is the GNU extension. The original awk still found as /bin/awk on Solaris, did not support support gsub(), but didn't not support a multi-character FS either. – Stéphane Chazelas Nov 22 '15 at 12:34

You require a solution in sed, and that is already given below. But I wanted to add another possibility that IMHO is just easier and faster,

tr -d "\n"
share|improve this answer
what does this do? delete all newlines from input? – mikeserv Aug 5 '14 at 21:51
Yes, that is exactly what it does – jpmuc Aug 6 '14 at 6:04
but... why? what does that have to do with the question at all? – mikeserv Aug 6 '14 at 6:44

If Holdspace is empty you can also do:

sed '/\\n/G;s/\\n\(.*\)\(.\)/\2\1/;P;D'

...but uxnut's answer is already both faster and more simple, so you can take it as you will.

Another extraneous possibility:

INPUT | sed -n l | while read v ; do printf "${v%?}" ; done

But beware, ^that translates all standard C-style \backslash escapes - like \backspace and \return and \00octals and whathaveyou.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.