Newline doesn't work in Sed on Mac OS X

时间:2022-09-10 16:47:37

'\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.