grep
文本搜索工具,根據(jù)用戶(hù)指定的“模式”對(duì)目標(biāo)文本逐行進(jìn)行匹配檢查;打印匹配到的行,常用選項(xiàng):
- --color=auto:關(guān)鍵字高亮顯示,在centos7當(dāng)中默認(rèn)做為別名
- -v:顯示沒(méi)有被匹配到的行
- -i:忽略大小寫(xiě)
- -n:顯示匹配的行號(hào)
- -c:顯示匹配行的數(shù)量
- -o:僅顯示匹配到的關(guān)鍵字
- -q:靜默模式,不輸出任何信息
- -A #:after,當(dāng)前行及后#行
- -B #: before, 當(dāng)前行及前#行
- -C #:context, x 當(dāng)前行及前后#行
- -e:表示邏輯或
- -e 關(guān)鍵字1 -e 關(guān)鍵字2 或者 "關(guān)鍵字1|關(guān)鍵字2" 表示邏輯或
- -w:示字符邊界,匹配整個(gè)單詞,也可以使用位置錨定<關(guān)鍵字>或\b關(guān)鍵字\b
- -E:使用擴(kuò)展正則表達(dá)式,相當(dāng)于egrep
- -F:不適用正則表達(dá)式,相當(dāng)于fgrep
查找172.18.118.xxx網(wǎng)段在線(xiàn)的主機(jī),只顯示在線(xiàn)主機(jī)的ip
[root@centos6 ~]# nmap -v -sP 172.18.118.0/24|grep -B1 "Host is up"|grep for|cut -d" " -f5
基本正則表達(dá)式
字符匹配
.:匹配任意單個(gè)字符
[]:匹配指定范圍內(nèi)的任意單個(gè)字符
[^]:匹配指定范圍外的任意單個(gè)字符
[:alnum:]:字母和數(shù)字
[:alpha:]:代表任何英文大小寫(xiě)字符,亦即 A-Z, a-z
[:lower:]:小寫(xiě)字母 [a-z]
[:upper:]:大寫(xiě)字母 [A-Z]
[:blank:]:空白字符(空格和制表符)
[:space:]:水平和垂直的空白字符(比[:blank:]包含的范圍廣)
[:cntrl:]:不可打印的控制字符(退格、刪除、警鈴...)
[:digit:]:十進(jìn)制數(shù)字 [0-9]
[:xdigit:]:十六進(jìn)制數(shù)字
[:graph:]:可打印的非空白字符
[:print:]:可打印字符(大小寫(xiě)字母、數(shù)字和標(biāo)點(diǎn)符號(hào))
[:punct:]:標(biāo)點(diǎn)符號(hào)
次數(shù)匹配
用在要指定次數(shù)的字符后面,用于指定前面的字符要出現(xiàn)的次數(shù)
*:匹配前面的字符任意次,包括0次(貪婪模式:盡可能長(zhǎng)的匹配)
\?:匹配其前面的字符0或1次
\+:匹配其前面的字符至少1次
\{n\}:匹配前面的字符n次
\{m,n\} 匹配前面的字符至少m次,至多n次
\{,n\} 匹配前面的字符至多n次
\{n,\} 匹配前面的字符至少n次
位置錨定
定位出現(xiàn)的位置
^:行首錨定,用于模式的最左側(cè)
$:行尾錨定,用于模式的最右側(cè)
\<或 \b:詞首錨定,用于單詞模式的左側(cè)
\> 或 \b:詞尾錨定,用于單詞模式的右側(cè)
[root@centos7 app]# cat test
root
rooter
[root@centos7 app]# cat test |grep -v "root"
[root@centos7 app]# cat test |grep -v "\<root\>"
rooter
分組
\(\):將一個(gè)或多個(gè)字符捆綁在一起,當(dāng)作一個(gè)整體,如\(root\)\+
分組括號(hào)中的模式匹配到的內(nèi)容會(huì)被正則表達(dá)式引擎記錄于內(nèi)部的變量中,這些變量的命名方式為:\1 \2 \3 ...
\(string1\+\(string2\)*\)
\1:string1\+\(string2\)*
\2:string2
或者
a\|b:a或b
C\|cat:C或cat
\(C\|c\)at:Cat或cat
練習(xí)
-
顯示/proc/meminfo文件中以大小s開(kāi)頭的行(要求:使用兩種方法)
[root@centos7 app]# grep "^\(s\|S\)" /proc/meminfo [root@centos7 app]# grep -i "^s" /proc/meminfo -
顯示/etc/passwd文件中不以/bin/bash結(jié)尾的行
[root@centos7 app]# grep -v "/bin/bash$" /etc/passwd -
顯示用戶(hù)rpc默認(rèn)的shell程序
[root@centos7 app]# getent passwd rpc|cut -d: -f1,7 -
找出/etc/passwd中的兩位或三位數(shù)
[root@centos7 app]# grep -o "\<[0-9]\{2,3\}\>" /etc/passwd -
顯示CentOS7的/etc/grub2.cfg文件中,至少以一個(gè)空白字符開(kāi)頭的且后面存非空白字符的行
[root@centos7 app]# cat /etc/grub2.cfg |grep "^[[:blank:]]\+.*" -
找出“netstat -tan”命令的結(jié)果中以‘LISTEN’后跟任意多個(gè)空白字符結(jié)尾的行
[root@centos7 app]# netstat -tan|grep "\<LISTEN\>[[:blank:]]\+" -
顯示CentOS7上所有UID小于1000的用戶(hù)的用戶(hù)名和UID
[root@centos7 app]# cat /etc/passwd|grep ".*:x:[0-9]\+:.*"|cut -d: -f1,3 -
添加用戶(hù)bash、testbash、basher、sh、nologin(其shell為/sbin/nologin),找出/etc/passwd用戶(hù)名同shell名的行
[root@centos7 app]# grep "^\(\<.*\>\):x:.*\1$" /etc/passwd -
利用df和grep,取出磁盤(pán)各分區(qū)利用率,并從大到小排序
[root@centos7 app]# df |grep -o "[0-9]\+%" |sort -nr
擴(kuò)展正則表達(dá)式
字符匹配
.:任意單個(gè)字符
[]:指定范圍的字符
[^] 不在指定范圍的字符
次數(shù)匹配
*:匹配前面字符任意次
?: 0或1次
+:1次或多次
{m}:匹配m次
{m,n}:至少m,至多n次
位置錨定
^:行首
$:行尾
\<,\b:語(yǔ)首
\>,\b:語(yǔ)尾
分組
() 后向引用:\1 \2 ...
或者
a|b:a或b
C|cat:C或cat
(C|c)at:Cat或cat
練習(xí)
-
顯示三個(gè)用戶(hù)root、sh、nologin的UID和默認(rèn)shell
[root@centos7 app]# cat /etc/passwd|grep "^\<\(root\|sh\|nologin\)\>"|cut -d: -f1,3,7 -
找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線(xiàn))后面跟一個(gè)小括號(hào)的行
[root@centos7 app]# cat /etc/rc.d/init.d/functions |grep "^\([[:alpha:]]\+_[[:alpha:]]\+\)(" 此表達(dá)式達(dá)不到指定的匹配要求 [root@centos7 app]# cat /etc/rc.d/init.d/functions |grep "^\([[:alpha:]]\|_\)\+(" -
使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@centos7 app]# echo /etc/rc.d/init.d/functions |egrep -o "[[:alpha:]]+$" [root@centos7 app]# echo /etc/rc.d/init.d/functions |egrep -o "[^/]+$" -
使用egrep取出上面路徑的目錄名
[root@centos7 app]# echo /etc/rc.d/init.d/functions |egrep -v "[[:alpha:]]\+$" /etc/rc.d/init.d/functions [root@centos7 app]# echo /etc/rc.d/init.d/functions |egrep -o "^/.*/.*/.*/" /etc/rc.d/init.d/ [root@centos7 app]# echo /etc/rc.d/init.d/functions |egrep -o "^/(([[:alpha:]]|.)+/)+" /etc/rc.d/init.d/ -
統(tǒng)計(jì)last命令中以root登錄的每個(gè)主機(jī)IP地址登錄次數(shù)
[root@centos7 app]# last |grep "^\<root\>.*pts/0"|tr -s " "|cut -d" " -f3|sort|uniq -c [root@centos7 app]# last |egrep -o "[0-9]+([[:punct:]])[0-9]+\1[0-9]+\1[0-9]+"|sort|uniq -c 15 172.18.118.140 -
顯示ifconfig命令結(jié)果中所有IPv4地址
[root@centos7 ~]# ifconfig |grep -o "inet [0-9]\+\([[:punct:]]\)[0-9]\+\1[0-9]\+\1[0-9]\+"|cut -d" " -f2 172.18.119.40 192.168.61.153 127.0.0.1 192.168.122.1 -
將此字符串:welcome to magedu linux 中的每個(gè)字符去重并排序,重復(fù)次數(shù)多的排到前面
[root@centos7 ~]# echo welcome to magedu linux |grep -o . |sort |uniq -c |sort -nr