[root@hf-01 grep]# grep 'r.o' passwd //会匹配出包含ro的字符,其中的点表示任意一个字符
[root@hf-01 grep]# grep 'r.o' passwd
root:x:0:0:root:/root:/bin/bash
adas:124:bdsf:rto:pass
halt:x:7:0:halt:/r.o:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost grep]# grep 'h*n' inittab
# inittab is no longer used when using systemd.
hhhhan
haaaaannnn
wannnnnnn
# Ctrl-Alt-Delete is handled by /etc/systemd/system/ctrl-alt-del.target
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
# To set a default target, run:
# ln -sf /lib/systemd/system/<target name>.target /etc/systemd/system/default.target
[root@hf-01 grep]#
总结grep 'hn' passwd 过滤的时候,跟号前面的字符相关,可以是0个或多个,跟后面的字符没有什么关系,若是在一行中无 h 而有 n 也会匹配出来(这就表示0个h)
[root@localhost grep]# grep '# inittab.*' inittab
# inittab is no longer used when using systemd.
[root@localhost grep]# grep 'h\{2\}' inittab
hhhhan
[root@localhost grep]#
[root@localhost grep]# grep 'h\{0,4\}' inittab //匹配文件中h出现0到4次的都会匹配出来
# inittab is no longer used when using systemd.
hhhhan
haaaaannnn
wannnnnnn
waaaaaal
# Ctrl-Alt-Delete is handled by /etc/systemd/system/ctrl-alt-del.target
#
等等等,只截取了一部分
[root@localhost grep]# grep -E 'h{2}' inittab
hhhhan
[root@localhost grep]# egrep 'h{2}' inittab
hhhhan
[root@localhost grep]#
[root@localhost grep]# grep -E '(hh){2}' inittab //表示连续的hh出现两次匹配打印出来
hhhhan
[root@localhost grep]# egrep '(hh){2}' inittab
hhhhan
grep -E等于egrep命令,若是在连续匹配字符的时候,不使用 grep -E或 egrep 命令,只使用grep命令,其中的符号则需要脱义。
[root@localhost grep]# egrep 'h+h' inittab
hhhhan
[root@localhost grep]# egrep 'h+a' inittab
hhhhan
haaaaannnn
# Ctrl-Alt-Delete is handled by /etc/systemd/system/ctrl-alt-del.target
[root@localhost grep]#
[root@localhost grep]# egrep 'h?1f' inittab //匹配h和1f字符,若文件没有h字符,则匹配1f字符
h1f:gfdgfg
hghjhk:1f:hjjkuhhj
[root@localhost grep]#
[root@localhost grep]# egrep 'h|1f' inittab
# inittab is no longer used when using systemd.
hhhhan
haaaaannnn
h1f:gfdgfg
hghjhk:1f:hjjkuhhj
# Ctrl-Alt-Delete is handled by /etc/systemd/system/ctrl-alt-del.target
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
# graphical.target: analogous to runlevel 5
[root@localhost grep]#