2018-1-15 6周1次課 grep
阿新 • • 發佈:2018-01-16
default /etc/ -m using ont port ssa term 並且 9.1 正則介紹_grep(上)
·正則就是一串有規律的字符串
·掌握好正則對於編寫shell腳本有很大幫助
·各種編程語言中都有正則,原理是一樣的
·本章將要學習grep/egrep、sed、awk
grep [-cinvABC] 'word' filename
-c 行數
-i 不區分大小寫
-n 顯示行號
-v 取反
-r 遍歷所有子目錄
-A 後面跟數字,過濾出符合要求的行以及下面n行
-B 同上,過濾出符合要求的行以及上面n行
-C 同上,同時過濾出符合要求的行以及上下各n行
[root@localhost ~]# mkdir grep [root@localhost ~]# cd grep/ [root@localhost grep]# cp /etc/passwd . [root@localhost grep]# ls passwd [root@localhost grep]# grep 'nologin' passwd bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:998:996::/var/lib/chrony:/sbin/nologin [root@localhost grep]# grep -c 'nologin' passwd ##-c 顯示行數 15 [root@localhost grep]# grep -n 'nologin' passwd ##-n 顯示行號 2:bin:x:1:1:bin:/bin:/sbin/nologin 3:daemon:x:2:2:daemon:/sbin:/sbin/nologin 4:adm:x:3:4:adm:/var/adm:/sbin/nologin 5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10:operator:x:11:0:operator:/root:/sbin/nologin 11:games:x:12:100:games:/usr/games:/sbin/nologin 12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 13:nobody:x:99:99:Nobody:/:/sbin/nologin 14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 15:dbus:x:81:81:System message bus:/:/sbin/nologin 16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin 17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin 18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin [root@localhost grep]# vim passwd ##vim中把第二行nologin改為NOLogin [root@localhost grep]# grep -n 'nologin' passwd ##大寫NOLogin就無法識別 3:daemon:x:2:2:daemon:/sbin:/sbin/nologin 4:adm:x:3:4:adm:/var/adm:/sbin/nologin 5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10:operator:x:11:0:operator:/root:/sbin/nologin 11:games:x:12:100:games:/usr/games:/sbin/nologin 12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 13:nobody:x:99:99:Nobody:/:/sbin/nologin 14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 15:dbus:x:81:81:System message bus:/:/sbin/nologin 16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin 17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin 18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin [root@localhost grep]# grep -in 'nologin' passwd ##-i 不區分大小寫,可識別第二行NOLogin 2:bin:x:1:1:bin:/bin:/sbin/NOLogin 3:daemon:x:2:2:daemon:/sbin:/sbin/nologin 4:adm:x:3:4:adm:/var/adm:/sbin/nologin 5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10:operator:x:11:0:operator:/root:/sbin/nologin 11:games:x:12:100:games:/usr/games:/sbin/nologin 12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 13:nobody:x:99:99:Nobody:/:/sbin/nologin 14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 15:dbus:x:81:81:System message bus:/:/sbin/nologin 16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin 17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin 18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin [root@localhost grep]# grep -inv 'nologin' passwd ##-v 取反,把不含nologin的行過濾出來 1:root:x:0:0:root:/root:/bin/bash 6:sync:x:5:0:sync:/sbin:/bin/sync 7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8:halt:x:7:0:halt:/sbin:/sbin/halt [root@localhost grep]# grep -r 'Streaming' /etc/##-r 遍歷/etc/目錄下,包含Streaming的行 /etc/services:nmsp 537/tcp # Networked Media Streaming Protocol /etc/services:nmsp 537/udp # Networked Media Streaming Protocol /etc/services:h263-video 2979/tcp # H.263 Video Streaming /etc/services:h263-video 2979/udp # H.263 Video Streaming /etc/services:daqstream 7411/tcp # Streaming of measurement /etc/services:daqstream 7411/udp # Streaming of measurement /etc/services:hp-pdl-datastr 9100/udp pdl-datastream # PDL Data Streaming Port /etc/services:bmdss 13823/tcp # Blackmagic Design Streaming Server 匹配到二進制文件 /etc/udev/hwdb.bin [root@localhost grep]# grep -r 'root' /etc/ > grep.txt | grep 'passwd' grep.txt /etc/passwd:root:x:0:0:root:/root:/bin/bash /etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin /etc/passwd-:root:x:0:0:root:/root:/bin/bash /etc/passwd-:operator:x:11:0:operator:/root:/sbin/nologin /etc/postfix/main.cf:# the system passwd file in the chroot jail is just not practical. [root@localhost grep]# grep -nA2 'root' passwd ##-A2 列出符合要求的行以及下面2行 1:root:x:0:0:root:/root:/bin/bash 2-bin:x:1:1:bin:/bin:/sbin/NOLogin 3-daemon:x:2:2:daemon:/sbin:/sbin/nologin -- 10:operator:x:11:0:operator:/root:/sbin/nologin 11-games:x:12:100:games:/usr/games:/sbin/nologin 12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin [root@localhost grep]# grep -nB2 'root' passwd ##-B2 列出符合要求的行以及上面2行 1:root:x:0:0:root:/root:/bin/bash -- 8-halt:x:7:0:halt:/sbin:/sbin/halt 9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10:operator:x:11:0:operator:/root:/sbin/nologin [root@localhost grep]# grep -nC2 'root' passwd ##-C2 列出符合要求的行以及上下2行 1:root:x:0:0:root:/root:/bin/bash 2-bin:x:1:1:bin:/bin:/sbin/NOLogin 3-daemon:x:2:2:daemon:/sbin:/sbin/nologin -- 8-halt:x:7:0:halt:/sbin:/sbin/halt 9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10:operator:x:11:0:operator:/root:/sbin/nologin 11-games:x:12:100:games:/usr/games:/sbin/nologin 12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
9.2 grep(中)
grep/egrep 示例
[root@localhost grep]# grep -n 'root' /etc/passwd##顯示行號 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin [root@localhost grep]# grep -nv 'root' /etc/passwd##顯示行號,並且取反 2:bin:x:1:1:bin:/bin:/sbin/nologin 3:daemon:x:2:2:daemon:/sbin:/sbin/nologin 4:adm:x:3:4:adm:/var/adm:/sbin/nologin 5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6:sync:x:5:0:sync:/sbin:/bin/sync 7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8:halt:x:7:0:halt:/sbin:/sbin/halt 9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 11:games:x:12:100:games:/usr/games:/sbin/nologin 12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 13:nobody:x:99:99:Nobody:/:/sbin/nologin 14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 15:dbus:x:81:81:System message bus:/:/sbin/nologin 16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin 17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin 18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin [root@localhost grep]# grep '[0-9]' /etc/inittab ##含有數字的行 # multi-user.target: analogous to runlevel 3 # graphical.target: analogous to runlevel 5 [root@localhost grep]# grep -v '[0-9]' /etc/inittab ##不含有數字的行 # inittab is no longer used when using systemd. # # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target # # systemd uses 'targets' instead of runlevels. By default, there are two main targets: # # # To view current default target, run: # systemctl get-default # # To set a default target, run: # systemctl set-default TARGET.target # [root@localhost grep]# grep -n '^#' /etc/inittab ##以#開頭的行 1:# inittab is no longer used when using systemd. 2:# 3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. 4:# 5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target 6:# 7:# systemd uses 'targets' instead of runlevels. By default, there are two main targets: 8:# 9:# multi-user.target: analogous to runlevel 3 10:# graphical.target: analogous to runlevel 5 11:# 12:# To view current default target, run: 13:# systemctl get-default 14:# 15:# To set a default target, run: 16:# systemctl set-default TARGET.target 17:# [root@localhost grep]# vim inittab ##加入一段胡亂的文字
[root@localhost grep]# grep -v '^#' inittab ##不以#開頭的行 &&*&*[]%% 999999999 aaaaaa jiojapjaoijfpaoijsdfpoali 77777a7777 [root@localhost grep]# grep -v '^#' inittab | grep -v '^&'##不以#開頭的行中,同時不以&開頭的行 999999999 aaaaaa jiojapjaoijfpaoijsdfpoali 77777a7777 [root@localhost grep]# grep '[^0-9]' inittab ##[^0-9]只要有不包含數字的行 # inittab is no longer used when using systemd. # # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # Ctrl-Alt-Delete is handled by /usr/lib/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 # aaaaaa # To view current default target, run: # systemctl get-default jiojapjaoijfpaoijsdfpoali # # To set a default target, run: # systemctl set-default TARGET.target 77777a7777 # [root@localhost grep]# grep '^[^0-9]' inittab ##以非數字開頭的行 # inittab is no longer used when using systemd. # # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # Ctrl-Alt-Delete is handled by /usr/lib/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 # aaaaaa # To view current default target, run: # systemctl get-default jiojapjaoijfpaoijsdfpoali # # To set a default target, run: # systemctl set-default TARGET.target # [root@localhost grep]# grep -v '^[^0-9]' inittab ##以數字開頭的行 999999999 77777a7777
9.3 grep(下)
. 一個任意字符
* *前面的字符重復0個或多次(*要和前面的字符組合起來用)
.* 通配,無論有沒有字符都匹配
{} 表示{}前面的字符重復次數範圍 ,{2}前面字符重復2次
+ +前面的字符重復1次或多次
? ?前面的字符重復0次或1次
| |表示或者
[root@localhost grep]# grep 'r.o' passwd ## . 匹配任何單個字符 root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin [root@localhost grep]# grep 'o*o' passwd ## **左邊的字符重復0-n次 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/NOLogin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin [root@localhost grep]# grep '.*' passwd ##任意一個單字符重復0-n次,就是任意字符 root:x:0:0:root:/root:/bin/bash ##.* 就是通配 bin:x:1:1:bin:/bin:/sbin/NOLogin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin [root@localhost grep]# grep 'o\{2\}' passwd ##兩個o root:x:0:0:root:/root:/bin/bash lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin [root@localhost grep]# grep 'o\{0,3\}' passwd ##o重復0-3次 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/NOLogin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin
·egrep = grep -E
如果不想用脫義符,那麽用egrep或grep -E
[root@localhost grep]# egrep 'o{2}' passwd root:x:0:0:root:/root:/bin/bash lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin [root@localhost grep]# grep -E 'o{2}' passwd root:x:0:0:root:/root:/bin/bash lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin [root@localhost grep]# egrep '(oo){2}' passwd ## oo字符重復兩次 postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin [root@localhost grep]# egrep 'o+t' passwd ## +前面的o出現一次或多次 root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@localhost grep]# egrep 'o?t' passwd ## ?前面的o出現0或1次 root:x:0:0:root:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt operator:x:11:0:operator:/root:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin [root@localhost grep]# egrep 'root|nologin' passwd ## | 表示或者,| 前後只要出現一個 root:x:0:0:root:/root:/bin/bash daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spooooool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:998:996::/var/lib/chroooony:/sbin/nologin
2018-1-15 6周1次課 grep