Newline doesn't work in Sed on Mac OS X

'\n' in the replace pattern in sed command doesn't work in Mac OS X.

On Debian, we can get bellowing result:

root# echo abc | sed 's/[^\n]/&\n/g'
a
b
c
root#

 

But on Mac, it goes like this:

user$ echo abc | sed 's/[^\n]/&\n/g'
anbncn
user$

 

Solution is to add \'$' before \n as bellowing:

user$ echo abc | sed 's/[^\n]/&\'$'\n/g'
a
b
c
user$

 

Please refer to http://superuser.com/questions/307165/newlines-in-sed-on-mac-os-x for more detail discussion.

But i tried another method sed 's/[^\n]/&\\\n/g' metioned in the discussion, it didn't work on my machine.

 

posted on 2013-01-28 16:02  name2579  阅读(252)  评论(0编辑  收藏  举报

导航