標籤:
Linux系統中grep命令是一種強大的文本搜尋工具,它能使用Regex搜尋文本,並把匹 配的行列印出來。grep全稱是Global Regular Expression Print,表示全域Regex版本,它的使用許可權是所有使用者。
grep的工作方式是這樣的,它在一個或多個檔案中搜尋字串模板。如果模板包括空格,則必須被引用,模板後的所有字串被看作檔案名稱。搜尋的結果被送到標準輸出,不影響原檔案內容。
grep可用於shell指令碼,因為grep通過返回一個狀態值來說明搜尋的狀態,如果模板搜尋成功,則返回0,如果搜尋不成功,則返回1,如果搜尋的檔案不存在,則返回2。我們利用這些傳回值就可進行一些自動化的文本處理工作。
1.命令格式:
grep [option] pattern file
2.命令功能:
用於過濾/搜尋的特定字元。可使用Regex能多種命令配合使用,使用上十分靈活。
3.命令參數:
-A<顯示行數> --after-context=<顯示行數> #除了顯示符合範本樣式的那一列之外,並顯示該行之後的內容。
-b --byte-offset #在顯示匹配行距檔案頭部的位移量。
-o --byte-offset #與-b配合使用,在顯示匹配的詞距檔案頭部的位移量
-B<顯示行數> --before-context=<顯示行數> #除了顯示符合樣式的那一行之外,並顯示該行之前的內容。
-c --count #計算符合樣式的行數。
-C<顯示行數> --context=<顯示行數>或-<顯示行數> #除了顯示符合樣式的那一行之外,並顯示該行之前後的內容。
-E --extended-regexp #將樣式為延伸的普通標記法來使用。
-F --fixed-regexp #將樣式視為固定字串的列表,即不支援Regex。
-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 #此參數遞迴尋找子目錄。
-s --no-messages #不顯示錯誤資訊。
-v --revert-match #顯示不包含匹配文本的所有行。
-V --version #顯示版本資訊。
-w --word-regexp #只顯示全字匹配的行。 也就是不把模式當做Regex。
-x --line-regexp #只有整行內容與模式比對時候,才輸出。
-y #此參數的效果和指定“-i”參數相同。
POSIX字元:
為了在不同國家的字元編碼中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字元類,如[:alnum:]是[A-Za-z0-9]的另一個寫法。要把它們放到[]號內才能成為Regex,如[A- Za-z0-9]或[[:alnum:]]。在linux下的grep除fgrep外,都支援POSIX的字元類。
[:alnum:] #文字數字字元
[:alpha:] #文字字元
[:digit:] #數字字元
[:graph:] #非Null 字元(非空格、控制字元)
[:lower:] #小寫字元
[:cntrl:] #控制字元
[:print:] #非Null 字元(包括空格)
[:punct:] #標點符號
[:space:] #所有空白字元(新行,空格,定位字元)
[:upper:] #大寫字元
[:xdigit:] #十六進位數字(0-9,a-f,A-F)
5.使用執行個體:
執行個體1:尋找指定進程
命令:
ps -ef|grep svn
輸出:
[[email protected] ~]# 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
[[email protected] ~]#
說明:
第一條記錄是尋找出的進程;第二條結果是grep進程本身,並非真正要找的進程。
執行個體2:尋找指定進程個數
命令:
ps -ef|grep svn -c
ps -ef|grep -c svn
輸出:
[[email protected] ~]# ps -ef|grep svn -c
2
[[email protected] ~]# ps -ef|grep -c svn
2
[[email protected] ~]#
說明:
執行個體3:從檔案中讀取關鍵詞進行搜尋
命令:
cat test.txt | grep -f test2.txt
輸出:
[[email protected] test]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[[email protected] test]# cat test2.txt
linux
Redhat
[[email protected] test]# cat test.txt | grep -f test2.txt
hnlinux
ubuntu linux
Redhat
linuxmint
[[email protected] test]#
說明:
輸出test.txt檔案中含有從test2.txt檔案中讀取出的關鍵詞的內容行
執行個體3:從檔案中讀取關鍵詞進行搜尋 且顯示行號
命令:
cat test.txt | grep -nf test2.txt
輸出:
[[email protected] test]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[[email protected] test]# cat test2.txt
linux
Redhat
[[email protected] test]# cat test.txt | grep -nf test2.txt
1:hnlinux
4:ubuntu linux
6:Redhat
7:linuxmint
[[email protected] test]#
說明:
輸出test.txt檔案中含有從test2.txt檔案中讀取出的關鍵詞的內容行,並顯示每一行的行號
執行個體5:從檔案中尋找關鍵詞
命令:
grep ‘linux‘ test.txt
輸出:
[[email protected] test]# grep ‘linux‘ test.txt
hnlinux
ubuntu linux
linuxmint
[[email protected] test]# grep -n ‘linux‘ test.txt
1:hnlinux
4:ubuntu linux
7:linuxmint
[[email protected] test]#
說明:
執行個體6:從多個檔案中尋找關鍵詞
命令:
grep ‘linux‘ test.txt test2.txt
輸出:
[[email protected] 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
[[email protected] test]# grep ‘linux‘ test.txt test2.txt
test.txt:hnlinux
test.txt:ubuntu linux
test.txt:linuxmint
test2.txt:linux
[[email protected] test]#
說明:
多檔案時,輸出查詢到的資訊內容行時,會把檔案的命名在行最前面輸出並且加上":"作為標示符
執行個體7:grep不顯示本身進程
命令:
ps aux|grep \[s]sh
ps aux | grep ssh | grep -v "grep"
輸出:
[[email protected] 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: [email protected]/0
root 16901 0.0 0.0 61180 764 pts/0 S+ 20:31 0:00 grep ssh
[[email protected] test]# ps aux|grep \[s]sh]
[[email protected] 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: [email protected]/0
[[email protected] 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: [email protected]/0
說明:
執行個體8:找出已u開頭的行內容
命令:
cat test.txt |grep ^u
輸出:
[[email protected] test]# cat test.txt |grep ^u
ubuntu
ubuntu linux
[[email protected] test]#
說明:
執行個體9:輸出非u開頭的行內容
命令:
cat test.txt |grep ^[^u]
輸出:
[[email protected] test]# cat test.txt |grep ^[^u]
hnlinux
peida.cnblogs.com
redhat
Redhat
linuxmint
[[email protected] test]#
說明:
執行個體10:輸出以hat結尾的行內容
命令:
cat test.txt |grep hat$
輸出:
[[email protected] test]# cat test.txt |grep hat$
redhat
Redhat
[[email protected] test]#
說明:
執行個體11:
命令:
輸出:
[[email protected] 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
[[email protected] 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
[[email protected] test]#
說明:
執行個體12:顯示包含ed或者at字元的內容行
命令:
cat test.txt |grep -E "ed|at"
輸出:
[[email protected] test]# cat test.txt |grep -E "peida|com"
peida.cnblogs.com
[[email protected] test]# cat test.txt |grep -E "ed|at"
redhat
Redhat
[[email protected] test]#
說明:
執行個體13:顯示目前的目錄下面以.txt 結尾的檔案中的所有包含每個字串至少有7個連續小寫字元的字串的行
命令:
grep ‘[a-z]\{7\}‘ *.txt
輸出:
[[email protected] test]# grep ‘[a-z]\{7\}‘ *.txt
test.txt:hnlinux
test.txt:peida.cnblogs.com
test.txt:linuxmint
[[email protected] test]#
linux sheel編程學習筆記(二) --- grep命令