MacOSX中的空间和换行符的变化

时间:2015-01-16 12:10:25

标签: regex linux macos sed

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

在LINUX上,我看到人们发布了诸如

之类的例子
sed 's/,\s/\n/g' textfile

然而在MacOSX中,这不起作用,但以下情况

sed 's/, /\
/g' testfile 

真实空间中的空格和换行符是一个实际的反斜杠,换行符为'输入'

我做错了什么?

1 个答案:

答案 0 :(得分:4)

在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