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'– glenn jackman Jul 06 '11 at 18:17\n. – Ivan Xiao Jul 06 '11 at 22:50echo "foo bar" | perl -pe 's/ /\n/orperl -pe 's/ +/\n/gto replace all spaces or groups of spaces by a new line. – mivk Sep 07 '20 at 12:26