wxin 发表于 2015-12-31 09:44:07

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.
  
页: [1]
查看完整版本: Newline doesn't work in Sed on Mac OS X