Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex MacOSX中sed随空格和换行符的变化_Regex_Linux_Macos_Sed - Fatal编程技术网

Regex MacOSX中sed随空格和换行符的变化

Regex MacOSX中sed随空格和换行符的变化,regex,linux,macos,sed,Regex,Linux,Macos,Sed,需要将逗号后跟空格替换为换行符 在LINUX上,我看到有人发布了如下示例 sed 's/,\s/\n/g' textfile 而在MacOSX中,这不起作用,但下面是 sed 's/, /\ /g' testfile 空格为实空格,换行符为实际反斜杠,换行符为“entered” 我做错了什么?在OSX上,您可以执行以下操作: sed -i.bak 's/, /\'$'\n''/g' file 或者这个: sed -i.bak $'s/, /\\\n/g' file 根据曼巴什: Wo

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

在LINUX上,我看到有人发布了如下示例

sed 's/,\s/\n/g' textfile
而在MacOSX中,这不起作用,但下面是

sed 's/, /\
/g' testfile 
空格为实空格,换行符为实际反斜杠,换行符为“entered”

我做错了什么?

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

sed -i.bak 's/, /\'$'\n''/g' file
或者这个:

sed -i.bak $'s/, /\\\n/g' file
根据
曼巴什

 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

OSX使用BSD版本的
sed
。大多数linux发行版(如果不是全部的话)都使用GNU版本的
sed
。他们的行为略有不同。您可以尝试使用
-E
标志,看看是否有效。或者为
\s
使用
[[:space:][]
应该可以,但我不确定替换中是否有
\n
的替代方法。啊,好的。那么,这一变化是否有记录在案?这意味着脚本编写也必须考虑到这一点。请解释一下->'$'\n''添加了一些关于
$字符串的使用说明。