grep (global search regular expression(RE) and print out the line,全面搜索正則表達式並把行打印出來)是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹配的行打印出來。
Unix的grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不同。egrep是grep的擴展,支持更多的re元字符, fgrep就是fixed grep或fast grep,它們把所有的字母都看作單詞,也就是說,正則表達式中的元字符表示回其自身的字面意義,不再特殊。linux使用GNU版本的grep。它功能更強,可以通過-G、-E、-F命令行選項來使用egrep和fgrep的
過濾文本grep
grep是一種強大的文本搜索工具命令,用於查找文件中的符合指定格式的字符串,支持正則表達式。如不指定任何文件名稱,或是所給予的文件名為“—”,則grep命令從標准輸入設備讀取數據。grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不同。egrep是grep的擴展。fgrep就是fixed grep或fast grep,該命令使用任何正則表達式中的元字符表示其自身的字面意義,不再特殊。其中egrep就等同於“grep —E”,fgrep等同於“grep -F”。Linux中的grep功能強大,支持很多豐富的參數,可以方便地進行一些文本處理工作。
grep單獨使用時至少有兩個參數,如少於兩個參數,grep會一直等待,直到該程序被中斷。如果遇到了這樣的情況,可以按“Ctrl+c”終止。默認情況下只搜索當前目錄,如果遞歸查找子目錄,可使用“r”選項。
#在指定文件中查找特定字符串 [root@CentOS ~]# grep root /etc/passwd #結合管道一起使用 [root@CentOS ~]# cat /etc/passwd | grep root #將顯示符合條件的內容所在的行號 [root@CentOS ~]# grep -n root /etc/passwd #在nginx.conf查找包含listen的行號打印出來 [root@CentOS conf]# grep listen nginx.conf #結合管道聯合使用,其中/sbin/ifconfig 表示查看當前系統的網絡配置信息,后查找包含“inetaddr"的字符串,第2行為查找的結果 [root@CentOS etc]# cat filel [root@CentOS etc]# grep var filel [root@CentOS etc]# grep -v var filel #顯示行號 [root@CentOS etc]# grep -n var filel [root@CentOS nginx]# /sbin/ifconfig | grep "inet addr" #綜合使用 $ grep magic /usr/src/linux/Documentation/* | tail #查看文件內容 [root@CentOS etc]# cat test.txt #查找指定字符串,此時是區分大小寫 [root@CentOS etc]# grep uuid test.txt [root@CentOS etc]# grep UUID test.txt #不區分大小寫查找指定字符串 [root@CentOS etc]# grep -i uuid test.txt #列出匹配字符串的文件名 [root@CentOS etc]# grep -l UUID test.txt [root@CentOS etc]# grep -L UUID test.txt #列出不匹配字符串的文件名 [root@CentOS etc]# grep -L uuid test.txt #匹配整個單詞 [root@CentOS etc]# grep -W UU test.txt [root@CentOS etc]# grep -W UUID test.txt #除了顯示匹配的行,分別顯示該行上下文的N行 [root@CentOS etc]# grep -C1 UUID test.txt [root@CentOS etc]# grep -n -E "^[a-z]+" test.txt [root@CentOS etc]# grep -n -E "^[^a-z]+" test.txt #按正則表達式查找指定字符串 [root@CentOS etc]# cat my.cnf #按正則表達式查找 [root@CentOS etc]# grep -E "datadir|socket"my.cnf [root@CentOS etc]# grep mysql my.cnf #結合管道一起使用 [root@CentOS etc]# grep mysql my.cnf | grep datadir #遞歸查找 [root@CentOS etc]# grep -r var .|head -3
反向查找,文件名中包含test 的文件中不包含test 的行
grep -v test*
例:在文件kkk中搜索匹配字符“test file”。
[root@rhel ~]# grep 'test file' kkk test file
例:在文件aa中顯示所有包含至少有5個連續小寫字符的行數據內容。
[root@rhel ~]# grep '[a-z]\{5\}' aa aaaaa aaaaaa
例:在/root/aa文件中找出以b開頭的行內容。
[root@rhel ~]# grep ^b /root/aa bbb
例:在/root/aa文件中輸出不是以b開頭的行內容。
[root@rhel ~]# grep -v ^b /root/aa aaaaa AAAAA BBB aaaaaa
例:在/root/kkk文件中輸出以le結尾的行內容。
[root@rhel ~]# grep le$ /root/kkk test file
在stdout1.log文件中查找有'exception'的行。
grep 'exception' stdout1.log
在stdout1.log文件中查找有'exception'的行的數目。
grep -c 'exception' stdout1.log
grep不顯示本身進程
ps aux|grep \[s]sh
ps aux | grep ssh | grep -v "grep"
輸出ip地址
ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]"
顯示當前目錄下面以.txt 結尾的文件中的所有包含每個字符串至少有7個連續小寫字符的字符串的行
grep '[a-z]\{7\}' *.txt
日志文件過大,不好查看,我們要從中查看自己想要的內容,或者得到同一類數據,比如說沒有404日志信息的
grep '.' access1.log|grep -Ev '404' > access2.log
grep '.' access1.log|grep -Ev '(404|/photo/|/css/)' > access2.log
grep '.' access1.log|grep -E '404' > access2.log
grep支持豐富的正則表達式,常見的正則元字符含義表
grep正則參數說明
| 參數 | 說明 |
| ^ | 指定匹配字符串的行首 |
| $ | 指定匹配字符串的結尾 |
| * | 表示0個以上的字符 |
| + | 表示1個以上的字符 |
| \ | 去掉指定字符的特殊含義 |
| ^ | 指定行的開始 |
| $ | 指定行的結束 |
| . | 匹配一個非換行符的字符 |
| * | 匹配零個或多個先前字符 |
| [] | 匹配一個指定范圍內的字符 |
| [^] | 匹配一個不在指定范圍內的字符 |
| \(..\) | 標記匹配字符 |
| < | 指定單詞的開始 |
| > | 指定單詞的結束 |
| 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] |
| b | 單詞鎖定符 |
| + | 匹配一個或多個先前的字符 |
| ? | 匹配零個或多個先前的字符 |
| a|b|c | 匹配a或b或c |
| () | 分組符號 |
| [:alnum:] | 文字數字字符 |
| [:alpha:] | 文字字符 |
| [:digit:] | 數字字符 |
| [:graph:] | 非空格、控制字符 |
| [:lower:] | 小寫字符 |
| [:cntrl:] | 控制字符 |
| [:print:] | 非空字符(包括空格) |
| [:punct:] | 標點符號 |
| [:space:] | 所有空白字符(新行,空格,制表符) |
| [:upper:] | 大寫字符 |
| [:xdigit:] | 十六進制數字(0-9,a-f,A-F) |
匹配的實例:
grep -c "48" test.txt 統計所有以“48”字符開頭的行有多少
grep -i "May" test.txt 不區分大小寫查找“May”所有的行)
grep -n "48" test.txt 顯示行號;顯示匹配字符“48”的行及行號,相同於 nl test.txt |grep 48)
grep -v "48" test.txt 顯示輸出沒有字符“48”所有的行)
grep "471" test.txt 顯示輸出字符“471”所在的行)
grep "48;" test.txt 顯示輸出以字符“48”開頭,並在字符“48”后是一個tab鍵所在的行
grep "48[34]" test.txt 顯示輸出以字符“48”開頭,第三個字符是“3”或是“4”的所有的行)
grep "^[^48]" test.txt 顯示輸出行首不是字符“48”的行)
grep "[Mm]ay" test.txt 設置大小寫查找:顯示輸出第一個字符以“M”或“m”開頭,以字符“ay”結束的行)
grep "K…D" test.txt 顯示輸出第一個字符是“K”,第二、三、四是任意字符,第五個字符是“D”所在的行)
grep "[A-Z][9]D" test.txt 顯示輸出第一個字符的范圍是“A-D”,第二個字符是“9”,第三個字符的是“D”的所有的行
grep "[35]..1998" test.txt 顯示第一個字符是3或5,第二三個字符是任意,以1998結尾的所有行
grep "4/{2,/}" test.txt 模式出現幾率查找:顯示輸出字符“4”至少重復出現兩次的所有行
grep "9/{3,/}" test.txt 模式出現幾率查找:顯示輸出字符“9”至少重復出現三次的所有行
grep "9/{2,3/}" test.txt 模式出現幾率查找:顯示輸出字符“9”重復出現的次數在一定范圍內,重復出現2次或3次所有行
grep -n "^$" test.txt 顯示輸出空行的行號
ls -l |grep "^d" 如果要查詢目錄列表中的目錄 同:ls -d *
ls -l |grep "^d[d]" 在一個目錄中查詢不包含目錄的所有文件
ls -l |grpe "^d…..x..x" 查詢其他用戶和用戶組成員有可執行權限的目錄集合
更多的例子:
搜索有the的行,並輸出行號
$grep -n 'the' regular_express.txt
搜 索沒有the的行,並輸出行號
$grep -nv 'the' regular_express.txt
利 用[]搜索集合字符
[] 表示其中的某一個字符 ,例如[ade] 表示a或d或e
woody@xiaoc:~/tmp$ grep -n 't[ae]st' regular_express.txt
8:I can't finish the test.
9:Oh! the soup taste good!
可以用^符號做[]內的前綴,表示除[]內的字符之外的字 符。
比如搜索oo前沒有g的字符串所在的行. 使用 '[^g]oo' 作搜索字符串
woody@xiaoc:~/tmp$ grep -n '[^g]oo' regular_express.txt
2:apple is my favorite food.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
19:goooooogle yes!
內可以用范圍表示,比如[a-z] 表示小寫字母,[0-9] 表示0~9的數字, [A-Z] 則是大寫字母們。[a-zA-Z0-9]表示所有數字與英文字符。 當然也可以配合^來排除字符。
搜索包含數字的行
woody@xiaoc:~/tmp$ grep -n '[0-9]' regular_express.txt
5:However ,this dress is about $ 3183 dollars.
15:You are the best is menu you are the no.1.
行首與行尾字符 ^ $. ^ 表示行的開頭,$表示行的結尾( 不是字符,是位置)那么‘^$’ 就表示空行,因為只有行首和行尾。
這里^與[]里面使用的^意義不同。它表示^后面的串是在行的開頭。
比如搜索the在開頭的行
woody@xiaoc:~/tmp$ grep -n '^the' regular_express.txt
12:the symbol '*' is represented as star.
搜索以小寫字母開頭的行
woody@xiaoc:~/tmp$ grep -n '^[a-z]' regular_express.txt
2:apple is my favorite food.
4:this dress doesn't fit me.
10:motorcycle is cheap than car.
12:the symbol '*' is represented as star.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
woody@xiaoc:~/tmp$
搜索開頭不是英文字母的行
woody@xiaoc:~/tmp$ grep -n '^[^a-zA-Z]' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
21:#I am VBird
woody@xiaoc:~/tmp$
$表示它前面的串是在行的結尾,比如 '/.' 表示 . 在一行的結尾
搜索末尾是.的行
woody@xiaoc:~/tmp$ grep -n '/.$' regular_express.txt //. 是正則表達式的特殊符號,所以要用/轉義
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
5:However ,this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
注意在MS的系統下生成的文本文件,換行會加上一個 ^M 字符。所以最后的字符會是隱藏的^M ,在處理Windows
下面的文本時要特別注意!
可以用cat dos_file | tr -d '/r' > unix_file 來刪除^M符號。 ^M==/r
那么'^$' 就表示只有行首行尾的空行拉!
搜索空行
woody@xiaoc:~/tmp$ grep -n '^$' regular_express.txt
22:
23:
woody@xiaoc:~/tmp$
搜索非空行
woody@xiaoc:~/tmp$ grep -vn '^$' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
