awk 正则表达式匹配错误?

awk 正则表达式匹配错误?

在使用 awk 时我注意到了这种行为:

[root@ror6ax3 ~]# grep open * | awk '$2 ~ /opens*/ {print $0}'
install.log:Installing openldap-2.4.23-32.el6_4.1.x86_64
install.log:Installing openssl-1.0.1e-15.el6.x86_64
install.log:Installing openssh-5.3p1-94.el6.x86_64
install.log:Installing openssh-clients-5.3p1-94.el6.x86_64
install.log:Installing openssh-server-5.3p1-94.el6.x86_64
install.log:Installing b43-openfwwf-5.2-4.el6.noarch
[root@ror6ax3 ~]# grep open * | awk '$2 ~ /opens */ {print $0}'
install.log:Installing openssl-1.0.1e-15.el6.x86_64
install.log:Installing openssh-5.3p1-94.el6.x86_64
install.log:Installing openssh-clients-5.3p1-94.el6.x86_64
install.log:Installing openssh-server-5.3p1-94.el6.x86_64

为什么会opens*匹配openldap

答案1

*表示 0 个或多个,因此实际上是 0 个或多个s字符。有文档这里, 说的是

例如,如果不存在“h”,则将ph*' applies the*' 符号添加到前面的h' and looks for matches of onep' 后跟任意数量的p'。h's. This also matches just

就您而言,您正在做的事情opens*可能是期待类似的事情opens+,其中+​​意味着“1或更多”。查看有关+操作员的文档这里

相关内容