Sed 的“+"量词似乎不匹配任何东西 [英] Sed's "+" quantifier does not seem to match anything
问题描述
我有一个包含内容的文件 sedTest.txt:
I have a file sedTest.txt with content:
sdjalsh ABC 123 ashfaladhs
ldjkasf ABC 234 dadas afsf
dafsafs ABC 876 asf dasfaf
我只想输出那些123
、234
、876
.
我可以使用
cat sedTest.txt | sed s"/.*ABC \([0-9]+\).*/\1/"
我是这样用的,因为通常有 grep 而不是 cat,在这种情况下可能是
I used it that way, because typically there is grep instead of cat, in this case it might be
grep ABC sedTest.txt | sed s"/.*ABC \([0-9]+\).*/\1/"
这在这里没有什么意义,但这是日志的典型用途.
which makes not really sense here, but this is typical use with logs.
我想知道:为什么使用 [0-9]+
不起作用?我希望得到与 [0-9]*
相同的结果,但似乎没有任何内容被替换(并打印未更改的内容).
I'm wondering: why is using [0-9]+
not working? I'd expect the same result as for [0-9]*
, but it seems that nothing is replaced (and unchanged content is printed).
推荐答案
+
是 ERE 元字符,sed 默认使用 BRE.使用 sed -E ... + ...
或 sed ... \+ ...
来启用 +
的 ERE 属性,例如
+
is an ERE metacharacter, sed uses BREs by default. Either use sed -E ... + ...
or sed ... \+ ...
to enable +
s ERE property, e.g.
grep 'ABC' sedTest.txt | sed -E 's/ABC ([0-9]+).*/\1/'
并注意,当您使用 -E
并且前导 .*
没有做任何有用的事情时,您不再需要转义括号以启用它们的 ERE 元字符属性,所以我摆脱了它.此外,您不需要 grep+sed,这样做:
and note that you no longer need to escape the parens to enable their ERE metacharacter properties when you use -E
and the leading .*
was doing nothing useful so I got rid of it. Also, you don't need grep+sed, this will do:
sed -E -n 's/ABC ([0-9]+).*/\1/p' sedTest.txt
但更重要的是你真的应该使用:
but more importantly you should really be using:
awk '$2=="ABC"{print $3}' sedTest.txt
而是为了清晰、简单、健壮、便携等.
instead for clarity, simplicity, robustness, portability, etc.
始终注意,您应该将脚本(sed、awk、grep 等)括在单引号 ('
) 中,而不是双引号 ("
) 中,除非您喜欢Shell 决定在您的工具看到它们之前解释脚本中的某些值.
Always note that you should enclose scripts (sed, awk, grep, whatever) in single quotes ('
), not double ("
) unless you enjoy surprises when the shell decides to interpret some values in your script before your tool sees them.
这篇关于Sed 的“+"量词似乎不匹配任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!