[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)php
[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命令,其中的符號則須要脫義。bash
[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]#