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 python輸出:
[yiibai@localhost yiibai]$ ps -ef|grep python
root        920      1  0 Feb25 ?        00:00:14 /usr/bin/python -Es /usr/sbin/tuned -l -P
root       4705   4631 91 08:05 pts/0    00:00:36 python run.py
yiibai     4726   4712  0 08:05 pts/0    00:00:00 grep --color=auto python
[yiibai@localhost yiibai]$說明:第一條記錄是查找出的進程;第二條結果是grep進程本身,並非真正要找的進程。
實例2:查找指定進程個數
命令:
ps -ef|grep python -c
ps -ef|grep -c python輸出:
[root@localhost yiibai]# ps -ef|grep python -c
3
[root@localhost yiibai]# ps -ef|grep -c python
3
[root@localhost yiibai]#實例3:從文件中讀取關鍵詞進行搜索
命令:
cat file2.txt |grep -f file1.txt輸出:
[yiibai@localhost ~]$ cat file1.txt
java
python
linux
[yiibai@localhost ~]$ cat file2.txt
windows 10
windows 7
linux
C#
java
PHP
python
1003
Hello Word.
[yiibai@localhost ~]$ cat file2 |grep -f file1.txt
cat: file2: No such file or directory
[yiibai@localhost ~]$ cat file2.txt |grep -f file1.txt
linux
java
python
[yiibai@localhost ~]$說明:輸出file2.txt文件中含有從file1.txt文件中讀取出的關鍵詞的內容行。
實例4:從文件中讀取關鍵詞進行搜索 且顯示行號
命令:
cat file2.txt | grep -nf file1.txt輸出:
[yiibai@localhost ~]$ cat file2.txt | grep -nf file1.txt
3:linux
5:java
7:python
[yiibai@localhost ~]$說明:輸出 file2.txt 文件中含有從 file1.txt 文件中讀取出的關鍵詞的內容行,並顯示每一行的行號。
實例5:從文件中查找關鍵詞
命令:
grep 'linux' file1.txt輸出:
[yiibai@localhost ~]$ grep 'linux' file1.txt
linux
[yiibai@localhost ~]$ grep 'java' file1.txt
java
[yiibai@localhost ~]$ grep 'ruby' file1.txt
[yiibai@localhost ~]$實例6:從多個文件中查找關鍵詞
命令:
grep 'linux' file1.txt file2.txt輸出:
[yiibai@localhost ~]$ grep 'linux' file1.txt file2.txt
file1.txt:linux
file2.txt:linux
[yiibai@localhost ~]$說明:多文件時,輸出查詢到的信息內容行時,會把文件的命名在行最前面輸出並且加上」:」作爲標示符。
實例7:grep不顯示本身進程
命令:
ps aux|grep \[s]sh
ps aux | grep ssh | grep -v "grep"輸出:
[yiibai@localhost ~]$ ps aux|grep \[s]sh
root       1219  0.0  0.1  82468  1332 ?        Ss   Feb25   0:00 /usr/sbin/sshd
root       4409  0.0  0.4 145700  4064 ?        Ss   07:28   0:00 sshd: yiibai [priv]
yiibai     4411  0.0  0.2 145700  2068 ?        S    07:29   0:05 sshd: yiibai@pts/0
[yiibai@localhost ~]$ ps aux | grep ssh | grep -v "grep"
root       1219  0.0  0.1  82468  1332 ?        Ss   Feb25   0:00 /usr/sbin/sshd
root       4409  0.0  0.4 145700  4064 ?        Ss   07:28   0:00 sshd: yiibai [priv]
yiibai     4411  0.0  0.2 145700  2068 ?        S    07:29   0:05 sshd: yiibai@pts/0
[yiibai@localhost ~]$實例8:找出已w開頭的行內容
命令:
cat file1.txt |grep ^w輸出:
[yiibai@localhost ~]$ cat file2.txt
windows 10
windows 7
linux
C#
java
PHP
python
1003
Hello Word.
[yiibai@localhost ~]$ cat file2.txt | grep ^w
windows 10
windows 7
[yiibai@localhost ~]$ cat file2.txt | grep ^p
python
[yiibai@localhost ~]$實例9:輸出非w開頭的行內容
命令:
cat file2.txt |grep ^[^w]輸出:
[yiibai@localhost ~]$ cat file2.txt
windows 10
windows 7
linux
C#
java
PHP
python
1003
Hello Word.
[yiibai@localhost ~]$ cat file2.txt |grep ^[^w]
linux
C#
java
PHP
python
1003
Hello Word.
[yiibai@localhost ~]$實例10:輸出以hat結尾的行內容
命令:
cat test.txt |grep hat$輸出:
實例11:顯示包含on或者on字符的內容行
命令:
cat file2.txt |grep -E "on|va"輸出:
[yiibai@localhost ~]$ cat file2.txt
windows 10
windows 7
linux
C#
java
PHP
python
1003
Hello Word.
[yiibai@localhost ~]$
[yiibai@localhost ~]$
[yiibai@localhost ~]$ cat file2.txt |grep -E "on|va"
java
python
[yiibai@localhost ~]$實例12:顯示當前目錄下面以.txt 結尾的文件中的所有包含每個字符串至少有7個連續小寫字符的字符串的行
命令:
grep '[a-z]\{7\}' *.txt輸出:
[yiibai@localhost ~]$ grep '[a-z]\{7\}' *.txt
file2.txt:windows 10
file2.txt:windows 7
[yiibai@localhost ~]$