如果我在我的 Linux 机器上执行以下 grep:
$ ps -ef | grep bash
root 2286 1 0 Jun06 ? 00:03:15 /bin/bash /etc/init.d/zxy100wd
wmiller 6436 6429 0 Jun06 pts/0 00:00:01 bash
wmiller 10707 6429 0 Jun07 pts/1 00:00:00 bash
wmiller 10795 6429 0 Jun07 pts/2 00:00:00 bash
wmiller 16220 6436 0 06:55 pts/0 00:00:00 grep --color=auto bash
请注意,最后一行报告 grep 本身,因为单词“bash”位于 grep 的参数中。
但是,如果我将 [] 放在“bash”中的任何字母周围,我会得到:
$ ps -ef | grep ba[s]h
root 2286 1 0 Jun06 ? 00:03:15 /bin/bash /etc/init.d/zxy100wd
wmiller 6436 6429 0 Jun06 pts/0 00:00:01 bash
wmiller 10707 6429 0 Jun07 pts/1 00:00:00 bash
wmiller 10795 6429 0 Jun07 pts/2 00:00:00 bash
这次没有关于 grep 的信息!
那么,为什么将搜索词中的字母(即正则表达式)括在括号中会阻止 grep 在这里报告自己呢?我认为 [s] 的意思是“由字符“s”组成的 [] 封闭集中的任何字符。
这是因为表达式ba[s]h
(or [b]ash
, 或...) 只是匹配bash
, not ba[s]h
(or [b]ash
, 或者...)。
So the grep
命令正在查找所有带有bash
:
root 2286 1 0 Jun06 ? 00:03:15 /bin/bash /etc/init.d/zxy100wd
wmiller 6436 6429 0 Jun06 pts/0 00:00:01 bash
wmiller 10707 6429 0 Jun07 pts/1 00:00:00 bash
wmiller 10795 6429 0 Jun07 pts/2 00:00:00 bash
but
wmiller 16220 6436 0 06:55 pts/0 00:00:00 grep --color=auto ba[s]h
不匹配,因为它不完全匹配bash
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)