当我使用 sed 并像这样使用大括号时
sed -re 's/top([0-9]+)//g'
我为此工作,但我看到有时我需要转义大括号,有时它无需转义就可以工作。这是为什么
为什么\w 不能与 awk 一起使用
最佳答案
这取决于您使用的 -r
开关:
-r, --regexp-extended
use extended regular expressions in the script.
对于 -r
,括号表示捕获的组,要匹配文字括号,您必须将它们转义。
没有 -r
反之亦然。
考虑:
$ sed -r 's/foo([0-9]*)/\1/' <<<'foo123'
123
$ sed 's/foo([0-9]*)/\1/' <<<'foo123'
sed: -e expression #1, char 17: invalid reference \1 on `s' command's RHS
$ sed 's/([0-9]*)//' <<<'foo(123)'
foo
$ sed 's/foo\([0-9]*\)/\1/' <<<'foo(123)'
(123)
(在最后一个组中匹配零位数字)
关于linux - 为什么我有时需要在 sed 中转义 (),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14829080/