六周第一次課
grep [-cinvABC] ‘word‘ filename
-c 行數
-i 不區分大小寫
-n 顯示行號
-v 取反
-r 遍歷所有子目錄
-A 後面跟數字,過濾出符合要求的行以及下面n行
-B 同上,過濾出符合要求的行以及上面n行
-C 同上,同時過濾出符合要求的行以及上下各n行
9.2 grep中
grep -n ‘root’ /root/passwd #過濾包含root的行並顯示行號
grep -nv ‘nologin’ /root/passwd #過濾不包含nologin的行並顯示行號
grep ‘[0-9]’ /root/inittab #查找包含數字的行
[root@Aiker ~]# grep ‘[0-9]‘ /root/inittab #multi-user.target: analogous to runlevel 3 #graphical.target: analogous to runlevel 5
grep -v ‘[0-9]’ /root/inittab #過濾不包含數字的行
[root@Aiker ~]# grep -v ‘[0-9]‘ /root/inittab #inittab is no longer used when using systemd. # #ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. grep -v ‘^#’ /root/inittab #過濾不包含#的行,^#不是#開頭的行 [root@Aiker ~]# grep -v ‘^#‘ /root/inittab Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target iewr# 35466
grep -v ‘^#’ /root/inittab|grep -v ‘^‘ #先過濾出不是#開頭的行,再在中間過濾出不是卡頭的行(第二個grep中不能用特殊符號 ^$ 表示空行,以空開頭 以空結尾 就是空行)
[root@Aiker ~]# grep -v ‘^#‘ /root/inittab | grep -v ‘^3‘ Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target $# multi-user.target: analogous to runlevel 3 iewr# $12312 [root@Aiker ~]# grep -v ‘^#‘ /root/inittab | grep -v ‘^i‘ Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target $# multi-user.target: analogous to runlevel 3 35466 $12312
grep ‘^[^a-zA-Z]’ /root/passwd #查出不含字母的含
[root@Aiker ~]# grep ‘^[^a-zA-Z]’ /root/passwd
234234235
&&
grep ‘m.s’ /root/passwd #“.”表示任意一個字符,查找m開頭+中間任意一個字符+s結尾
[root@Aiker ~]# grep ‘m.s‘ /root/passwd
games:x:12:100:games:/usr/games:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
mysql:x:1017:1017::/home/mysql:/sbin/mologin
grep ‘oo’ /root/passwd # “”表示0個或多個前面這個字符
grep ‘oo’ /root/passwd #左邊的這個字符重復0到n次,一次是‘oo’,兩次是‘ooo’
這裏寫圖片描述
grep ‘.’ /root/passwd #匹配所有字符串
grep ‘o{2}’ /root/passwd #匹配o出現2次 等於 grep -E ‘o{2}’ /root/passwd
[root@Aiker ~]# grep ‘o\{2\}‘ /root/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/spool/postfix:/sbin/nologin
roooot:#45345
toooos:2423
[root@Aiker ~]# grep ‘o\{3\}‘ /root/passwd
roooot:#45345
toooos:2423
這裏寫圖片描述
egrep可以不脫意{},grep -E也可以達到egrep效果
egrep ‘o{2}’ /root/passwd #效果同grep ‘o{2}’ /root/passwd
egrep ‘o+t’ /root/passwd # +這個符號前面的重復一次或者n次,*號是0次或者多次
這裏寫圖片描述
egrep ‘oo?’ /root/passwd #?前面這個字符重復次數為0或者1
egrep ‘root|nologin’ /root/passwd #包含root或者nologin的行
egrep ‘(oo){2}’ /root/passwd #連續兩個00在一起的行
[root@Aiker ~]# egrep ‘(oo){2}‘ /root/passwd
roooot:#45345
toooos:2423
9.3 grep下
grep -v ‘^#‘ /etc/inittab 不顯示#開頭的行也就是不顯示註釋行
grep -Ev ‘^$|^[#;]‘ inittab 過濾註釋和空行
grep -v ‘^#‘ /etc/inittab|grep -v ‘^$‘ 效果同上
grep ‘^[^a-zA-Z]‘ test.txt 顯示非字母開頭的行內容
grep -n ‘^[0-9]‘ inittab 顯示數字開頭的行內容和行號
grep -n ‘^[^0-9]‘ inittab 顯示非數字開頭的行內容和行號
grep ‘r.o‘ test.txt 匹配除換行符 \n 之外的任何單字符。要匹配 . ,請使用 .
grep ‘oo‘ test.txt 匹配前面的子表達式零次或多次。要匹配 字符,請使用 *。
grep ‘.‘ test.txt 匹配任意字符
grep ‘o{2}‘ /etc/passwd 匹配2次,{}表示範圍
egrep ‘o{2}‘ /etc/passwd 同上
egrep ‘o+‘ /etc/passwd 匹配+前的字符一次或者多個
egrep ‘oo?‘ /etc/passwd 匹配一個或者0個
egrep ‘root|nologin‘ /etc/passwd |表示或者
egrep ‘(oo){2}‘ /etc/passwd ()表示一個整體
擴展
把一個目錄下,過濾所有.php文檔中含有eval的行
grep -r --include="*.php" ‘eval‘ /data/
六周第一次課