这个sed命令如何工作?...... sed -E' s /(^ | [^ - ]) - ([^ - ] | $)/ \ 1xx \ 2 / g'

时间:2017-10-27 16:03:07

标签: sed

sed -E 's/(^|[^-])--([^-]|$)/\1xx\2/g'

我发现这个命令很难理解 我甚至找不到-E下的man sed选项,但该命令有效。 替换没有意义,因为它查看流的开头,然后看起来立即管道。

非常感谢任何解释。

2 个答案:

答案 0 :(得分:2)

只要不是三个或更多短划线的一部分,它就会将--替换为xx|不是管道,它是另一种选择。 (^|[^-])表示"该行的开头,或者某个字符不是-",而([^-]|$)表示"或某些字符那不是-,或者是行的结尾"。然后,输出中包含这些组捕获的内容,不变,因为我们只想将它们与上下文匹配。

答案 1 :(得分:0)

不确定您正在使用的sed实施方式。

看看gnu实现。

参考:https://www.gnu.org/software/sed/manual/sed.txt

'-E'
'-r'
'--regexp-extended'
    Use extended regular expressions rather than basic regular
    expressions.  Extended regexps are those that 'egrep' accepts; they
    can be clearer because they usually have fewer backslashes.
    Historically this was a GNU extension, but the '-E' extension has
    since been added to the POSIX standard
    (http://austingroupbugs.net/view.php?id=528), so use '-E' for
    portability.  GNU sed has accepted '-E' as an undocumented option
    for years, and *BSD seds have accepted '-E' for years as well, but
    scripts that use '-E' might not port to other older systems.  *Note
    Extended regular expressions: ERE syntax.