天天看點

每天一個linux指令:grep 指令

Linux系統中grep指令是一種強大的文本搜尋工具,它能使用正規表達式搜尋文本,并把匹 配的行列印出來。grep全稱是Global Regular Expression Print,表示全局正規表達式版本,它的使用權限是所有使用者。

grep的工作方式是這樣的,它在一個或多個檔案中搜尋字元串模闆。如果模闆包括空格,則必須被引用,模闆後的所有字元串被看作檔案名。搜尋的結果被送到标準輸出,不影響原檔案内容。

grep可用于shell腳本,因為grep通過傳回一個狀态值來說明搜尋的狀态,如果模闆搜尋成功,則傳回0,如果搜尋不成功,則傳回1,如果搜尋的檔案不存在,則傳回2。我們利用這些傳回值就可進行一些自動化的文本處理工作。

1.指令格式:

grep [option] pattern file

2.指令功能:

用于過濾/搜尋的特定字元。可使用正規表達式能多種指令配合使用,使用上十分靈活。

3.指令參數:

-a   --text   #不要忽略二進制的資料。   

-A<顯示行數>   --after-context=<顯示行數>   #除了顯示符合範本樣式的那一列之外,并顯示該行之後的内容。   

-b   --byte-offset   #在顯示符合樣式的那一行之前,标示出該行第一個字元的編号。   

-B<顯示行數>   --before-context=<顯示行數>   #除了顯示符合樣式的那一行之外,并顯示該行之前的内容。   

-c    --count   #計算符合樣式的列數。   

-C<顯示行數>    --context=<顯示行數>或-<顯示行數>   #除了顯示符合樣式的那一行之外,并顯示該行之前後的内容。   

-d <動作>      --directories=<動作>   #當指定要查找的是目錄而非檔案時,必須使用這項參數,否則grep指令将回報資訊并停止動作。   

-e<範本樣式>  --regexp=<範本樣式>   #指定字元串做為查找檔案内容的樣式。   

-E      --extended-regexp   #将樣式為延伸的普通表示法來使用。   

-f<規則檔案>  --file=<規則檔案>   #指定規則檔案,其内容含有一個或多個規則樣式,讓grep查找符合規則條件的檔案内容,格式為每行一個規則樣式。   

-F   --fixed-regexp   #将樣式視為固定字元串的清單。   

-G   --basic-regexp   #将樣式視為普通的表示法來使用。   

-h   --no-filename   #在顯示符合樣式的那一行之前,不标示該行所屬的檔案名稱。   

-H   --with-filename   #在顯示符合樣式的那一行之前,表示該行所屬的檔案名稱。   

-i    --ignore-case   #忽略字元大小寫的差别。   

-l    --file-with-matches   #列出檔案内容符合指定的樣式的檔案名稱。   

-L   --files-without-match   #列出檔案内容不符合指定的樣式的檔案名稱。   

-n   --line-number   #在顯示符合樣式的那一行之前,标示出該行的列數編号。   

-q   --quiet或--silent   #不顯示任何資訊。   

-r   --recursive   #此參數的效果和指定“-d recurse”參數相同。   

-s   --no-messages   #不顯示錯誤資訊。   

-v   --revert-match   #顯示不包含比對文本的所有行。   

-V   --version   #顯示版本資訊。   

-w   --word-regexp   #隻顯示全字元合的列。   

-x    --line-regexp   #隻顯示全列符合的列。   

-y   #此參數的效果和指定“-i”參數相同。

4.規則表達式:

grep的規則表達式:

^  #錨定行的開始 如:'^grep'比對所有以grep開頭的行。    

$  #錨定行的結束 如:'grep$'比對所有以grep結尾的行。    

.  #比對一個非換行符的字元 如:'gr.p'比對gr後接一個任意字元,然後是p。    

*  #比對零個或多個先前字元 如:'*grep'比對所有一個或多個空格後緊跟grep的行。    

.*  #一起用代表任意字元。   

[]  #比對一個指定範圍内的字元,如'[Gg]rep'比對Grep和grep。    

[^]  #比對一個不在指定範圍内的字元,如:'[^A-FH-Z]rep'比對不包含A-R和T-Z的一個字母開頭,緊跟rep的行。    

\(..\)  #标記比對字元,如'\(love\)',love被标記為1。    

\<      #錨定單詞的開始,如:'\<grep'比對包含以grep開頭的單詞的行。    

\>      #錨定單詞的結束,如'grep\>'比對包含以grep結尾的單詞的行。    

x\{m\}  #重複字元x,m次,如:'0\{5\}'比對包含5個o的行。    

x\{m,\}  #重複字元x,至少m次,如:'o\{5,\}'比對至少有5個o的行。    

x\{m,n\}  #重複字元x,至少m次,不多于n次,如:'o\{5,10\}'比對5--10個o的行。   

\w    #比對文字和數字字元,也就是[A-Za-z0-9],如:'G\w*p'比對以G後跟零個或多個文字或數字字元,然後是p。   

\W    #\w的反置形式,比對一個或多個非單詞字元,如點号句号等。   

\b    #單詞鎖定符,如: '\bgrep\b'隻比對grep。  

POSIX字元:

為了在不同國家的字元編碼中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字元類,如[:alnum:]是[A-Za-z0-9]的另一個寫法。要把它們放到[]号内才能成為正規表達式,如[A- Za-z0-9]或[[:alnum:]]。在linux下的grep除fgrep外,都支援POSIX的字元類。

[:alnum:]    #文字數字字元   

[:alpha:]    #文字字元   

[:digit:]    #數字字元   

[:graph:]    #非空字元(非空格、控制字元)   

[:lower:]    #小寫字元   

[:cntrl:]    #控制字元   

[:print:]    #非空字元(包括空格)   

[:punct:]    #标點符号   

[:space:]    #所有空白字元(新行,空格,制表符)   

[:upper:]    #大寫字元   

[:xdigit:]   #十六進制數字(0-9,a-f,A-F)  

5.使用執行個體:

執行個體1:查找指定程序

指令:

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 ~]#

說明:

第一條記錄是查找出的程序;第二條結果是grep程序本身,并非真正要找的程序。

執行個體2:查找指定程序個數

指令:

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 ~]#

說明:

執行個體3:從檔案中讀取關鍵詞進行搜尋

指令:

cat test.txt | grep -f test2.txt

輸出:

[root@localhost test]# cat test.txt 

hnlinux

peida.cnblogs.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檔案中含有從test2.txt檔案中讀取出的關鍵詞的内容行

執行個體3:從檔案中讀取關鍵詞進行搜尋 且顯示行号

指令:

cat test.txt | grep -nf test2.txt

輸出:

[root@localhost test]# cat test.txt 

hnlinux

peida.cnblogs.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檔案中含有從test2.txt檔案中讀取出的關鍵詞的内容行,并顯示每一行的行号

執行個體5:從檔案中查找關鍵詞

指令:

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]#

說明:

執行個體6:從多個檔案中查找關鍵詞

指令:

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]#

說明:

多檔案時,輸出查詢到的資訊内容行時,會把檔案的命名在行最前面輸出并且加上":"作為标示符

執行個體7:grep不顯示本身程序

指令:

ps aux|grep \[s]sh

ps aux | grep ssh | grep -v "grep"

輸出:

[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

說明:

執行個體8:找出已u開頭的行内容

指令:

cat test.txt |grep ^u

輸出:

[root@localhost test]# cat test.txt |grep ^u

ubuntu

ubuntu linux

[root@localhost test]#

說明:

執行個體9:輸出非u開頭的行内容

指令:

cat test.txt |grep ^[^u]

輸出:

[root@localhost test]# cat test.txt |grep ^[^u]

hnlinux

peida.cnblogs.com

redhat

Redhat

linuxmint

[root@localhost test]#

說明:

執行個體10:輸出以hat結尾的行内容

指令:

cat test.txt |grep hat$

輸出:

[root@localhost test]# cat test.txt |grep hat$

redhat

Redhat

[root@localhost test]#

說明:

執行個體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:顯示包含ed或者at字元的内容行

指令:

cat test.txt |grep -E "ed|at"

輸出:

[root@localhost test]# cat test.txt |grep -E "peida|com"

peida.cnblogs.com

[root@localhost test]# cat test.txt |grep -E "ed|at"

redhat

Redhat

[root@localhost test]#

說明:

執行個體13:顯示目前目錄下面以.txt 結尾的檔案中的所有包含每個字元串至少有7個連續小寫字元的字元串的行

指令:

grep '[a-z]\{7\}' *.txt

輸出:

[root@localhost test]# grep '[a-z]\{7\}' *.txt

test.txt:hnlinux

test.txt:peida.cnblogs.com

test.txt:linuxmint

[root@localhost test]#

參考資料:

linux基礎

快速上手linux

Linux入門基礎教程

Linux入門學習方法

linux之C語言記憶體管理