sed重复\ +不适用于macOS但+适用于linux

时间:2015-12-09 13:46:44

标签: regex macos bash sed

我有一个内容为<meta name="Description" content="The official site ......">

的文件

我尝试使用 sed 命令将其更改为:<meta name="hello" content="The official site ......">

sed -i -E 's/Descript[^"]\+/hello/g' index.html

但它不起作用,我把它改为:

sed -i -E 's/Descript[^"]*/hello/g' index.html

它有效,为什么重复\+不起作用? (我也试过+

修改

我正在使用OSX El Capitan,我只是在linux(ubuntu和fedora)上试过sed -i -r 's/Descript[^"]+/hello/g',它可以工作。

在Linux上它说:

  

-r, - rengexp-extended

          use extended regular expressions in the script.
在OSX上它说:

  

-E将正则表达式解释为扩展(现代)常规                表达式而不是基本的正则表达式(BRE)。该                re_format(7)手册页完全描述了两种格式。

修改

这里变得更奇怪了:

danny:~ mac$ echo '<meta name="Description" content="The official....' | sed  -E 's/Descript[^"]+/hello/' 
<meta name="hello" content="The official....
danny:~ mac$ 
danny:~ mac$ echo '<meta name="Description" content="The official....' | sed  -E 's/Descript[^"]\+/hello/' 
<meta name="Description" content="The official....

1 个答案:

答案 0 :(得分:0)

使用+(不含backslash)即可。 \+将与文字“加号”匹配。                      - 肯特

BSD sed需要-i之后的参数。所以即使sed -i '' -E 's/Descript[^"]+/hello/g' index.html也会奏效。                      - anubhava

请参阅man sedman re_formatDifferences between sed on Mac OSX and other “standard” sed?