MacOSX 中带有 space 和换行符的 sed 变体

sed variation in MacOSX with space and newline

需要将 space 后面的逗号替换为换行符

在 LINUX 上,我看到有人张贴示例,例如

sed 's/,\s/\n/g' textfile

虽然在 MacOSX 中这行不通,但以下行

sed 's/, /\
/g' testfile 

space 和真正的 space 一样,换行符是一个带有换行符的实际反斜杠 'entered'

我做错了什么?

在 OSX 你可以做:

sed -i.bak 's/, /\'$'\n''/g' file

或者这样:

sed -i.bak $'s/, /\\n/g' file

根据man bash

 Words  of  the  form  $'string'  are  treated specially.  The word expands to string, 
 with backslash-escaped characters replaced as specified by the ANSI C standard. 
 Backslash escape sequences, if present, are decoded as follows:
          \a     alert (bell)
          \b     backspace
          \e     an escape character
          \f     form feed
          \n     new line
          \r     carriage return
          \t     horizontal tab
          \v     vertical tab
          \     backslash
          \'     single quote
          \nnn   the eight-bit character whose value is the octal value nnn
                 (one to three digits)
          \xHH   the eight-bit character whose value is the hexadecimal value HH
                 (one or two hex digits)
          \cx    a control-x character