每天(tian)一(yi)個linux命(ming)令(39):grep 命(ming)令
Linux系統中(zhong)grep命令是一種強大的文本(ben)搜索工具(ju),它(ta)能(neng)使(shi)用正則表(biao)達(da)式(shi)搜索文本(ben),并把(ba)匹 配的行打印(yin)出來。grep全稱(cheng)是Global Regular Expression Print,表(biao)示(shi)全局正則表(biao)達(da)式(shi)版本(ben),它(ta)的使(shi)用權(quan)限是所(suo)有用戶(hu)。
grep的(de)工作方式是這樣的(de),它在一個或多個文件中(zhong)搜(sou)索字符(fu)串模(mo)板。如(ru)果模(mo)板包括空格,則必須被(bei)引用,模(mo)板后的(de)所有字符(fu)串被(bei)看(kan)作文件名(ming)。搜(sou)索的(de)結果被(bei)送(song)到標(biao)準輸出,不(bu)影(ying)響原文件內容。
grep可用于shell腳本(ben),因(yin)為(wei)grep通過返(fan)(fan)(fan)回(hui)一個狀(zhuang)態值來說明搜(sou)索的狀(zhuang)態,如果(guo)模(mo)板搜(sou)索成功(gong)(gong),則返(fan)(fan)(fan)回(hui)0,如果(guo)搜(sou)索不(bu)成功(gong)(gong),則返(fan)(fan)(fan)回(hui)1,如果(guo)搜(sou)索的文件(jian)不(bu)存(cun)在,則返(fan)(fan)(fan)回(hui)2。我們(men)利用這些返(fan)(fan)(fan)回(hui)值就可進行一些自動化的文本(ben)處理(li)工作。
1.命令格(ge)式:
grep [option] pattern file
2.命令功能:
用于(yu)過濾/搜索的特定字符。可使(shi)用正(zheng)則(ze)表達式能多種命令配(pei)合使(shi)用,使(shi)用上十分靈活。
3.命令參數(shu):
-a --text #不(bu)要忽略二進制的(de)數據。
-A<顯(xian)(xian)(xian)示(shi)行(xing)(xing)數> --after-context=<顯(xian)(xian)(xian)示(shi)行(xing)(xing)數> #除了顯(xian)(xian)(xian)示(shi)符(fu)合范本樣(yang)式的那(nei)一列之(zhi)(zhi)外,并顯(xian)(xian)(xian)示(shi)該(gai)行(xing)(xing)之(zhi)(zhi)后的內容。
-b --byte-offset #在顯示符(fu)合(he)樣式的(de)那一(yi)行之前,標(biao)示出該行第一(yi)個字符(fu)的(de)編號。
-B<顯示(shi)行(xing)數> --before-context=<顯示(shi)行(xing)數> #除了顯示(shi)符合(he)樣式(shi)的(de)那一行(xing)之外(wai),并顯示(shi)該行(xing)之前(qian)的(de)內容。
-c --count #計算符合樣式(shi)的列數。
-C<顯(xian)示(shi)行數(shu)(shu)> --context=<顯(xian)示(shi)行數(shu)(shu)>或-<顯(xian)示(shi)行數(shu)(shu)> #除了顯(xian)示(shi)符合樣式的(de)那一行之外,并顯(xian)示(shi)該(gai)行之前(qian)后的(de)內容。
-d <動作(zuo)> --directories=<動作(zuo)> #當指定要查(cha)找的是目錄而非文件時,必須(xu)使用(yong)這項參數,否(fou)則grep指令將回報信息并(bing)停(ting)止動作(zuo)。
-e<范本樣式(shi)(shi)> --regexp=<范本樣式(shi)(shi)> #指定字符串做為(wei)查找(zhao)文件(jian)內容的樣式(shi)(shi)。
-E --extended-regexp #將(jiang)樣(yang)式為延伸的普通表示(shi)法(fa)來使用。
-f<規(gui)(gui)則(ze)(ze)文件(jian)> --file=<規(gui)(gui)則(ze)(ze)文件(jian)> #指(zhi)定(ding)規(gui)(gui)則(ze)(ze)文件(jian),其內(nei)容(rong)含有一個(ge)或多個(ge)規(gui)(gui)則(ze)(ze)樣(yang)式(shi),讓grep查(cha)找符合規(gui)(gui)則(ze)(ze)條件(jian)的文件(jian)內(nei)容(rong),格式(shi)為每行一個(ge)規(gui)(gui)則(ze)(ze)樣(yang)式(shi)。
-F --fixed-regexp #將樣(yang)式視為(wei)固定(ding)字符串的(de)列表。
-G --basic-regexp #將(jiang)樣式(shi)視為(wei)普通(tong)的表示法來使用。
-h --no-filename #在顯示符合樣(yang)式的那(nei)一行之前,不(bu)標示該(gai)行所(suo)屬(shu)的文件名(ming)稱(cheng)。
-H --with-filename #在顯示符(fu)合樣式的(de)那一行之前,表示該行所屬的(de)文(wen)件名稱。
-i --ignore-case #忽略字符大小寫的差(cha)別。
-l --file-with-matches #列出文件(jian)內容符合指定的(de)樣式的(de)文件(jian)名稱(cheng)。
-L --files-without-match #列出文件內容不符合指定的樣式的文件名稱。
-n --line-number #在顯示(shi)符合樣式的那一(yi)行之前,標示(shi)出該行的列(lie)數(shu)編號(hao)。
-q --quiet或--silent #不顯示任何信(xin)息。
-r --recursive #此參數(shu)(shu)的效果和指定“-d recurse”參數(shu)(shu)相同。
-s --no-messages #不顯示錯誤信(xin)息(xi)。
-v --revert-match #顯示(shi)不包含匹配文本的(de)所(suo)有(you)行。
-V --version #顯示(shi)版本信息。
-w --word-regexp #只顯(xian)示全字(zi)符合的(de)列(lie)。
-x --line-regexp #只(zhi)顯示(shi)全列符合的(de)列。
-y #此參(can)數(shu)的效果和指定“-i”參(can)數(shu)相同。
4.規則表達式:
grep的規則表達式(shi):
^ #錨定行的(de)開始 如:'^grep'匹配所有(you)以grep開頭的(de)行。
$ #錨定行的(de)(de)結(jie)束(shu) 如:'grep$'匹配所有以grep結(jie)尾(wei)的(de)(de)行。
. #匹(pi)配一(yi)個非換行符(fu)(fu)的字符(fu)(fu) 如:'gr.p'匹(pi)配gr后接一(yi)個任(ren)意字符(fu)(fu),然后是p。
* #匹配(pei)零個(ge)或多個(ge)先前字符 如:'*grep'匹配(pei)所有(you)一個(ge)或多個(ge)空(kong)格后緊跟(gen)grep的行。
.* #一(yi)起用代表(biao)任意字(zi)符(fu)。
[] #匹配(pei)一(yi)個指定范圍內的字符,如'[Gg]rep'匹配(pei)Grep和grep。
[^] #匹配(pei)一個(ge)(ge)不在指(zhi)定范圍內的(de)(de)字符,如:'[^A-FH-Z]rep'匹配(pei)不包含A-R和T-Z的(de)(de)一個(ge)(ge)字母開(kai)頭,緊(jin)跟(gen)rep的(de)(de)行。
\(..\) #標(biao)記(ji)匹(pi)配(pei)字(zi)符,如(ru)'\(love\)',love被(bei)標(biao)記(ji)為1。
\< #錨定單詞(ci)的開(kai)始,如(ru):'\<grep'匹配包含(han)以(yi)grep開(kai)頭的單詞(ci)的行。
\> #錨定單(dan)詞的結束,如'grep\>'匹(pi)配包含以grep結尾的單(dan)詞的行。
x\{m\} #重復字(zi)符x,m次,如(ru):'0\{5\}'匹(pi)配(pei)包含5個o的行(xing)。
x\{m,\} #重復字符(fu)x,至少m次,如:'o\{5,\}'匹(pi)配(pei)至少有5個o的行。
x\{m,n\} #重復字符x,至少m次,不(bu)多于n次,如:'o\{5,10\}'匹配5--10個(ge)o的行(xing)。
\w #匹配(pei)文(wen)字(zi)(zi)(zi)和數(shu)字(zi)(zi)(zi)字(zi)(zi)(zi)符,也就是[A-Za-z0-9],如:'G\w*p'匹配(pei)以G后(hou)跟零個或(huo)多個文(wen)字(zi)(zi)(zi)或(huo)數(shu)字(zi)(zi)(zi)字(zi)(zi)(zi)符,然后(hou)是p。
\W #\w的反置形式,匹配一個或多個非單(dan)詞字符,如點號句號等(deng)。
\b #單(dan)詞(ci)鎖定符,如: '\bgrep\b'只匹(pi)配(pei)grep。
POSIX字符:
為(wei)了在不同國家的(de)字(zi)符編碼中(zhong)保持一(yi)至,POSIX(The Portable Operating System Interface)增加(jia)了特殊的(de)字(zi)符類,如[:alnum:]是(shi)[A-Za-z0-9]的(de)另一(yi)個寫法(fa)。要把它們放到[]號內才能成為(wei)正則表達式,如[A- Za-z0-9]或[[:alnum:]]。在linux下的(de)grep除(chu)fgrep外,都支持POSIX的(de)字(zi)符類。
[:alnum:] #文字(zi)數字(zi)字(zi)符
[:alpha:] #文字字符
[:digit:] #數字字符
[:graph:] #非(fei)空(kong)字符(非(fei)空(kong)格、控制字符)
[:lower:] #小寫字符
[:cntrl:] #控制字符(fu)
[:print:] #非空字符(包(bao)括(kuo)空格(ge))
[:punct:] #標(biao)點(dian)符號
[:space:] #所有空白字符(fu)(新行,空格,制表符(fu))
[:upper:] #大寫(xie)字符
[:xdigit:] #十六(liu)進制數字(0-9,a-f,A-F)
5.使用實例:
實例1:查(cha)找指定進程
命令:
ps -ef|grep svn
輸出:
[root@localhost ~]# ps -ef|grep svn
root 4943 1 0 Dec05 ? 00:00:00 svnserve -d -r /opt/svndata/grape/
root 16867 16838 0 19:53 pts/0 00:00:00 grep svn
[root@localhost ~]#
說明(ming):
第一條記錄(lu)是查找出的進程(cheng);第二條(tiao)結果是grep進(jin)程本身,并非真正(zheng)要找的進(jin)程。
實例2:查找指定進程個數
命(ming)令:
ps -ef|grep svn -c
ps -ef|grep -c svn
輸出:
[root@localhost ~]# ps -ef|grep svn -c
2
[root@localhost ~]# ps -ef|grep -c svn
2
[root@localhost ~]#
說(shuo)明(ming):
實例3:從文件中讀(du)取關鍵詞進行搜索
命令(ling):
cat test.txt | grep -f test2.txt
輸出:
[root@localhost test]# cat test.txt
hnlinux
peida.ywjunkang.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt
linux
Redhat
[root@localhost test]# cat test.txt | grep -f test2.txt
hnlinux
ubuntu linux
Redhat
linuxmint
[root@localhost test]#
說明:
輸出test.txt文件中含有(you)從test2.txt文件中讀取出的(de)關鍵詞的(de)內(nei)容(rong)行(xing)
實例3:從文件中讀(du)取關鍵詞(ci)進行搜索 且顯示行號
命令:
cat test.txt | grep -nf test2.txt
輸出:
[root@localhost test]# cat test.txt
hnlinux
peida.ywjunkang.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt
linux
Redhat
[root@localhost test]# cat test.txt | grep -nf test2.txt
1:hnlinux
4:ubuntu linux
6:Redhat
7:linuxmint
[root@localhost test]#
說明:
輸出test.txt文(wen)件中含有從test2.txt文(wen)件中讀取出的(de)關鍵詞的(de)內容行(xing),并顯示每(mei)一行(xing)的(de)行(xing)號
實例5:從文件(jian)中查找關鍵詞(ci)
命令:
grep 'linux' test.txt
輸出:
[root@localhost test]# grep 'linux' test.txt
hnlinux
ubuntu linux
linuxmint
[root@localhost test]# grep -n 'linux' test.txt
1:hnlinux
4:ubuntu linux
7:linuxmint
[root@localhost test]#
說明(ming):
實例6:從多個文件中查找關(guan)鍵詞
命令:
grep 'linux' test.txt test2.txt
輸出:
[root@localhost test]# grep -n 'linux' test.txt test2.txt
test.txt:1:hnlinux
test.txt:4:ubuntu linux
test.txt:7:linuxmint
test2.txt:1:linux
[root@localhost test]# grep 'linux' test.txt test2.txt
test.txt:hnlinux
test.txt:ubuntu linux
test.txt:linuxmint
test2.txt:linux
[root@localhost test]#
說明:
多文(wen)件(jian)時(shi),輸出查詢到的(de)信息內容行(xing)時(shi),會把(ba)文(wen)件(jian)的(de)命名(ming)在行(xing)最前面輸出并且加(jia)上":"作為標示符
實例7:grep不顯示本身進(jin)程
命令:
ps aux|grep \[s]sh
ps aux | grep ssh | grep -v "grep"
輸(shu)出:
[root@localhost test]# ps aux|grep ssh
root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
root 16901 0.0 0.0 61180 764 pts/0 S+ 20:31 0:00 grep ssh
[root@localhost test]# ps aux|grep \[s]sh]
[root@localhost test]# ps aux|grep \[s]sh
root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
[root@localhost test]# ps aux | grep ssh | grep -v "grep"
root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
說(shuo)明(ming):
實例8:找出已u開(kai)頭(tou)的行內容(rong)
命(ming)令:
cat test.txt |grep ^u
輸出:
[root@localhost test]# cat test.txt |grep ^u
ubuntu
ubuntu linux
[root@localhost test]#
說(shuo)明:
實例9:輸(shu)出(chu)非(fei)u開頭(tou)的行內容
命令(ling):
cat test.txt |grep ^[^u]
輸出:
[root@localhost test]# cat test.txt |grep ^[^u]
hnlinux
peida.ywjunkang.com
redhat
Redhat
linuxmint
[root@localhost test]#
說明:
實(shi)例(li)10:輸出以hat結(jie)尾的行內容(rong)
命令(ling):
cat test.txt |grep hat$
輸(shu)出(chu):
[root@localhost test]# cat test.txt |grep hat$
redhat
Redhat
[root@localhost test]#
說明(ming):
實例11:
命令:
輸出:
[root@localhost test]# ifconfig eth0|grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
[root@localhost test]# ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]"
inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
[root@localhost test]#
說明:
實例12:顯示(shi)包(bao)含ed或者at字符的內容行
命令:
cat test.txt |grep -E "ed|at"
輸出:
[root@localhost test]# cat test.txt |grep -E "peida|com"
peida.ywjunkang.com
[root@localhost test]# cat test.txt |grep -E "ed|at"
redhat
Redhat
[root@localhost test]#
說(shuo)明:
實例13:顯示(shi)當(dang)前目錄(lu)下面以.txt 結(jie)尾(wei)的(de)文(wen)件中(zhong)的(de)所有包含每個字符串至少有7個連續(xu)小(xiao)寫字符(fu)的字符(fu)串的行
命令:
grep '[a-z]\{7\}' *.txt
輸(shu)出:
[root@localhost test]# grep '[a-z]\{7\}' *.txt
test.txt:hnlinux
test.txt:peida.ywjunkang.com
test.txt:linuxmint
[root@localhost test]#
說(shuo)明(ming):
關注 熵減黑客 ,一起學習成長
