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

Bas*_*wer 2 regex linux macos sed

需要用换行符替换逗号后跟空格

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

sed 's/,\s/\n/g' textfile
Run Code Online (Sandbox Code Playgroud)

在MacOSX中,此功能无效,但以下功能可以

sed 's/, /\
/g' testfile 
Run Code Online (Sandbox Code Playgroud)

实际空间中的空间和换行符是实际的反斜杠,换行符“已输入”

我究竟做错了什么 ?

anu*_*ava 5

在OSX上,您可以执行以下操作:

sed -i.bak 's/, /\'$'\n''/g' file
Run Code Online (Sandbox Code Playgroud)

或这个:

sed -i.bak $'s/, /\\\n/g' file
Run Code Online (Sandbox Code Playgroud)

按照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
Run Code Online (Sandbox Code Playgroud)