For whatever reason, this is harder than it should have been. All I wanted to do was replace a particular expression with a newline character (0x10). My preference is typically to script such tasks, and the sed command is the perfect fit. This would be simple on just about any system, except for Mac OS X, where apparently all the standard advice is difficult to apply. Worse, the sed man page leads you to believe it’s a very simple matter of putting a newline in the replacement string. Of course, there’s no explanation as to how you are expected to do that. And not being a bash expert, I was at a loss.
Luckily enough I found a blog post that discussed, among other things, how to inject a newline using sed on Mac. Although the example is rather complicated given that he’s solving a different problem, the crux of the matter is this expression: $'\n/g'
$ echo 'foo bar baz quux' | sed -e 's/ /\'$'\n/g' foo bar baz quux
All that is really doing is taking advantage of the bash extquote option where $'string' quoting is performed on the enclosed string. In this case it’s a \n which expands to a newline character, followed by /g which goes through as-is. The baskslash (\) before the $'\n/g' tells sed to escape the newline character in the replacement string. I’m no expert here, but my understanding is that the argument to sed consists of two parts, the s/ /\ and the [newline]/g, where the latter resulted from the $'\n/g' evaluated by bash. Together this forms the sed expression s/ /\[newline]/g. How the \' doesn’t escape the quote and go through as-is to sed I’m not sure. Maybe someone can explain in the comments.
Thanks for reading!
Damn it… I encounter this problem too. This is so hard to understand.
Pingback: links for 2011-07-06 « Use You Imagination
I’m having this problem on OsX 10.6 not just with sed but with bash in general. No matter how I try to get a script to output a newline, all it does is print \n.
The issue here is not with OSX per-se as it is with the default version of sed you get with OSX.
/usr/bin/sed is the BSD version from May 10, 2005
Optionally, you can install gnu sed or gsed via ports (currently 4.2.1 from June 2009) that behaves as you are accustomed to on linux.
$ echo “a b c d” | gsed ‘s/ /\n/g’
a
b
c
d
i love you. thank you.
Pingback: You see, yesterday I referred to some work I had to do for university. That wasn’t quite true. - GhostLyrics' Journal