I find that \n
doesn't work in sed under Mac OS X.
Specifically, say I want to break the words separated by a single space into lines:
# input
foo bar
I use,
echo "foo bar" | sed 's/ /\n/'
But the result is stupid, the \n
is not escaped!
foonbar
After I consulted to google, I found a workaround:
echo 'foo bar' | sed -e 's/ /\'$'\n/g'
After reading the article, I still cannot understand what \'$'\n/g'
means.
Can some one explain it to me, or if there is any other way to do it? Thanks!
echo "foo bar" | tr ' ' '\n'
\n
.echo "foo bar" | perl -pe 's/ /\n/
orperl -pe 's/ +/\n/g
to replace all spaces or groups of spaces by a new line.