Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

It's a general problem when I use sed but \n works when I use it outside of sed. This is one example of what I get with sed.

I have the file myfile with the following content:

1 2 3 4 5

When I use this:

sed 's/ /\n/g' myfile

I get this:


Instead of getting this:


Does anybody know where the problem is?

share|improve this question
I could not reproduce this. Can you post the output of sed --version and bash --version, please? – jofel Jun 7 '14 at 16:52
I have tried it and it worked, are you sure that there is a problem ? – Networker Jun 7 '14 at 16:57
@Networker I do have the problem but I do not know where it comes from. – Charlie Jun 7 '14 at 16:58
@jofel Here is to reproduce myfile: echo "1 2 3 4 5" > myfile and the output I get is $ sed 's/ /\n/g' myfile 1n2n3n4n5 – Charlie Jun 7 '14 at 17:01
@Charlie: What is your OS? – cuonglm Jun 7 '14 at 17:03

You are using OSX 10.8, so your sed implement is FreeBSD sed, not GNU sed. So FreeBSD sed does not interpret \n as newline.

You can try:

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

$'\n/g' causes bash to interpret the string as standard escape expansion, so \n is converted to newline before sed process.

or using tr instead of sed:

$ tr -s ' ' '\n' < myfile
share|improve this answer
Thanks Gnouc it works perfectly when I use: $ sed 's/ /\'$'\n/g' myfile Is GNUsed implemented for higher or lower versions of OSX? – Charlie Jun 7 '14 at 17:29
@Charlie: you can install gsed for GNU sed implement in OSX. – cuonglm Jun 7 '14 at 17:30
@Gnouc - you've been here for a while and write up very solid A's. As one of our regulars if you feel like sharing we'd all like to get to know you more, there's a meta Q that many of us have written up more detailed bios about ourselves. meta.unix.stackexchange.com/questions/2668/… – slm Jun 8 '14 at 1:02
@slm: I have not known about that meta Q yet. I wrote my answer. Thanks for letting me know that. – cuonglm Jun 8 '14 at 8:05

Gnouc is spot on about the cause, but the portable way to specify a \n on the right hand side of an s/// statement is:


But you can also Get a \newline appended to the end of your pattern space from a blank hold space like:


And you can portably use the y translate operator with the \n on the righthand side as well. So your ...

sed 's/ /\n/g' myfile

... would do what you expected if it were written like...

sed 'y/ /\n/' myfile

... with the added advantage that it would probably also do it just a tiny bit faster. Though Gnouc's tr solution would likely be much faster still.

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.