了解带有*的基本正则表达式和grep组合

时间:2019-01-22 22:09:17

标签: regex bash grep

这是我的文件

$ cat delete1
KEEPALIVED-MIB.txt:     virtualServerRateCps Gauge32,
KEEPALIVED-MIB.txt:    SYNTAX INTEGER { tcp(1), udp(2) }
KEEPALIVED-MIB.txt:virtualServerRateCps OBJECT-TYPE
KEEPALIVED-MIB.txt:     realServerRateCps Gauge32,
KEEPALIVED-MIB.txt:realServerRateCps OBJECT-TYPE
KEEPALIVED-MIB.txt:     virtualServerRateCps,
KEEPALIVED-MIB.txt:     realServerRateCps,
MF-Diameter-MIB.txt:                tcp(1),

如果我搜索'cpu',则找不到符合我期望的任何匹配项。

$ grep -i 'cpu' delete1

但是,如果我添加通配符'cpu*',则会找到匹配项。但是我不确定为什么吗? 有人可以告诉我为什么我在这里找到比赛吗?

$ grep -i 'cpu*' delete1
KEEPALIVED-MIB.txt:     virtualServerRateCps Gauge32,
KEEPALIVED-MIB.txt:    SYNTAX INTEGER { tcp(1), udp(2) }
KEEPALIVED-MIB.txt:virtualServerRateCps OBJECT-TYPE
KEEPALIVED-MIB.txt:     realServerRateCps Gauge32,
KEEPALIVED-MIB.txt:realServerRateCps OBJECT-TYPE
KEEPALIVED-MIB.txt:     virtualServerRateCps,
KEEPALIVED-MIB.txt:     realServerRateCps,
MF-Diameter-MIB.txt:                tcp(1),

我已经读过一些docs,我在想它是否找不到cpu,不确定是否可以找到cpucpuuuuuu

a*  ---> a{0,}  ---> Match a or aa or aaaaa or an empty string

1 个答案:

答案 0 :(得分:0)

我认为这可以解释。

$ cat delete
cpu1
cpu2
cpu
cp

$ grep -i 'cpu' delete
cpu1
cpu2
cpu

这正在寻找cpucpuuuucp

$ grep -i 'cpu*' delete
cpu1
cpu2
cpu
cp