linuxgrep命令詳解

linux grep命令詳解

簡(jiǎn)介

grep (global search regular expression(RE) and print out the line,全面搜索正則表達(dá)式并把行打印出來(lái))是一種強(qiáng)大的文本搜索工具,它能使用正則表達(dá)式搜索文本,并把匹配的行打印出來(lái)。

十余年的深州網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營(yíng)銷的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整深州建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“深州網(wǎng)站設(shè)計(jì)”,“深州網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

Unix的grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不同。egrep是grep的擴(kuò)展,支持更多的re元字符, fgrep就是fixed grep或fast grep,它們把所有的字母都看作單詞,也就是說(shuō),正則表達(dá)式中的元字符表示回其自身的字面意義,不再特殊。linux使用GNU版本的grep。它功能更強(qiáng),可以通過(guò)-G、-E、-F命令行選項(xiàng)來(lái)使用egrep和fgrep的功能。

 

grep常用用法

linux grep命令詳解
[root@www ~]# grep [-acinv] [--color=auto] '搜尋字符串' filename
選項(xiàng)與參數(shù):-a :將 binary 文件以 text 文件的方式搜尋數(shù)據(jù)-c :計(jì)算找到 '搜尋字符串' 的次數(shù)-i :忽略大小寫的不同,所以大小寫視為相同-n :順便輸出行號(hào)-v :反向選擇,亦即顯示出沒有 '搜尋字符串' 內(nèi)容的那一行!--color=auto :可以將找到的關(guān)鍵詞部分加上顏色的顯示喔!
linux grep命令詳解
 

將/etc/passwd,有出現(xiàn) root 的行取出來(lái)

linux grep命令詳解
# grep root /etc/passwd root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
或# cat /etc/passwd | grep root 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
linux grep命令詳解

 

將/etc/passwd,有出現(xiàn) root 的行取出來(lái),同時(shí)顯示這些行在/etc/passwd的行號(hào)

# grep -n root /etc/passwd  1:root:x:0:0:root:/root:/bin/bash30:operator:x:11:0:operator:/root:/sbin/nologin

 在關(guān)鍵字的顯示方面,grep 可以使用 --color=auto 來(lái)將關(guān)鍵字部分使用顏色顯示。 這可是個(gè)很不錯(cuò)的功能??!但是如果每次使用 grep 都得要自行加上 --color=auto 又顯的很麻煩~ 此時(shí)那個(gè)好用的 alias 就得來(lái)處理一下啦!你可以在 ~/.bashrc 內(nèi)加上這行:『alias grep='grep --color=auto'』再以『 source ~/.bashrc 』來(lái)立即生效即可喔! 這樣每次運(yùn)行 grep 他都會(huì)自動(dòng)幫你加上顏色顯示

 

將/etc/passwd,將沒有出現(xiàn) root 的行取出來(lái)

# grep -v root /etc/passwd root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

 

將/etc/passwd,將沒有出現(xiàn) root 和nologin的行取出來(lái)

# grep -v root /etc/passwd | grep -v nologin
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

 

用 dmesg 列出核心信息,再以 grep 找出內(nèi)含 eth 那行,要將捉到的關(guān)鍵字顯色,且加上行號(hào)來(lái)表示:

[root@www ~]# dmesg | grep -n --color=auto 'eth'  247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10248:eth0: Identified 8139 chip type 'RTL-8139C' 294:eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1 305:eth0: no IPv6 routers present
# 你會(huì)發(fā)現(xiàn)除了 eth 會(huì)有特殊顏色來(lái)表示之外,最前面還有行號(hào)喔!

用 dmesg 列出核心信息,再以 grep 找出內(nèi)含 eth 那行,在關(guān)鍵字所在行的前兩行與后三行也一起捉出來(lái)顯示

linux grep命令詳解
[root@www ~]# dmesg | grep -n -A3 -B2 --color=auto 'eth' 245-PCI: setting IRQ 10 as level-triggered 246-ACPI: PCI Interrupt 0000:00:0e.0[A] -> Link [LNKB] ...247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10 248:eth0: Identified 8139 chip type 'RTL-8139C' 249-input: PC Speaker as /class/input/input2 250-ACPI: PCI Interrupt 0000:00:01.4[B] -> Link [LNKB] ... 251-hdb: ATAPI 48X DVD-ROM DVD-R-RAM CD-R/RW drive, 2048kB Cache, UDMA(66)
# 如上所示,你會(huì)發(fā)現(xiàn)關(guān)鍵字247 所在的前兩行及 248 后三行也都被顯示出來(lái)!
# 這樣可以讓你將關(guān)鍵字前后數(shù)據(jù)捉出來(lái)進(jìn)行分析啦!
linux grep命令詳解

 

根據(jù)文件內(nèi)容遞歸查找目錄

# grep ‘energywise’ *           #在當(dāng)前目錄搜索帶'energywise'行的文件

#grep -r ‘energywise’ *        #在當(dāng)前目錄及其子目錄下搜索'energywise'行的文件
#grep -l -r ‘energywise’ *     #在當(dāng)前目錄及其子目錄下搜索'energywise'行的文件,但是不顯示匹配的行,只顯示匹配的文件

這幾個(gè)命令很使用,是查找文件的利器。

 

grep與正規(guī)表達(dá)式

 一:字符類
1)

字符類的搜索:如果我想要搜尋 test 或 taste 這兩個(gè)單字時(shí),可以發(fā)現(xiàn)到,其實(shí)她們有共通的 't?st' 存在~這個(gè)時(shí)候,我可以這樣來(lái)搜尋:

[root@www ~]# grep -n 't[ae]st' regular_express.txt 8:I can't finish the test.  9:Oh! The soup taste good.
其實(shí) [] 里面不論有幾個(gè)字節(jié),他都謹(jǐn)代表某『一個(gè)』字節(jié), 所以,上面的例子說(shuō)明了,我需要的字串是『tast』或『test』兩個(gè)字串而已!


2)

字符類的反向選擇 [^]:如果想要搜索到有 oo 的行,但不想要 oo 前面有 g,如下

[root@www ~]# grep -n '[^g]oo' regular_express.txt2: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!

第 2,3 行沒有疑問,因?yàn)?foo 與 Foo 均可被接受!

但是第 18 行明明有 google 的 goo 啊~別忘記了,因?yàn)樵撔泻竺娉霈F(xiàn)了 tool 的 too ??!所以該行也被列出來(lái)~ 也就是說(shuō), 18 行里面雖然出現(xiàn)了我們所不要的項(xiàng)目 (goo) 但是由於有需要的項(xiàng)目 (too) , 因此,是符合字串搜尋的喔!

至於第 19 行,同樣的,因?yàn)?goooooogle 里面的 oo 前面可能是 o ,例如: go(ooo)oogle ,所以,這一行也是符合需求的!


3)

字符類的連續(xù):再來(lái),假設(shè)我 oo 前面不想要有小寫字節(jié),所以,我可以這樣寫 [^abcd....z]oo , 但是這樣似乎不怎么方便,由於小寫字節(jié)的 ASCII 上編碼的順序是連續(xù)的, 因此,我們可以將之簡(jiǎn)化為底下這樣:

[root@www ~]# grep -n '[^a-z]oo' regular_express.txt
 3:Football game is not use feet only.

也就是說(shuō),當(dāng)我們?cè)谝唤M集合字節(jié)中,如果該字節(jié)組是連續(xù)的,例如大寫英文/小寫英文/數(shù)字等等, 就可以使用[a-z],[A-Z],[0-9]等方式來(lái)書寫,那么如果我們的要求字串是數(shù)字與英文呢? 呵呵!就將他全部寫在一起,變成:[a-zA-Z0-9]。

我們要取得有數(shù)字的那一行,就這樣:

[root@www ~]# grep -n '[0-9]' regular_express.txt5:However, this dress is about $ 3183 dollars.15:You are the best is mean you are the no. 1.


4)

行首與行尾字節(jié) ^ $
行首字符:如果我想要讓 the 只在行首列出呢? 這個(gè)時(shí)候就得要使用定位字節(jié)了!我們可以這樣做:'^the'

[root@www ~]# grep -n '^the' regular_express.
txt12:the symbol '*' is represented as start.


此時(shí),就只剩下第 12 行,因?yàn)橹挥械?12 行的行首是 the 開頭啊~此外, 如果我想要開頭是小寫字節(jié)的那一行就列出呢?可以這樣:

linux grep命令詳解
[root@www ~]# grep -n '^[a-z]' regular_express.txt2:apple is my favorite food. 4:this dress doesn't fit me.  10:motorcycle is cheap than car.12:the symbol '*' is represented as start. 18:google is the best tools for search keyword.19:goooooogle yes!  20:go! go! Let's go.
linux grep命令詳解
如果我不想要開頭是英文字母,則可以是這樣:
[root@www ~]# grep -n '^[^a-zA-Z]' regular_express.txt1:"Open Source" is a good mechanism to develop programs.21:# I am VBird

^ 符號(hào),在字符類符號(hào)(括號(hào)[])之內(nèi)與之外是不同的!在 [] 內(nèi)代表『反向選擇』,在 [] 之外則代表定位在行首的意義!

 

那如果我想要找出來(lái),行尾結(jié)束為小數(shù)點(diǎn) (.) 的那一行:

linux grep命令詳解
[root@www ~]# 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. 10:motorcycle is cheap than car.11:This window is clear.12:the symbol '*' is represented as start.15:You are the best is mean you are the no. 1.16:The world <Happy> is the same with "glad". 17:I like dog.18:google is the best tools for search keyword.20:go! go! Let's go.
linux grep命令詳解


特別注意到,因?yàn)樾?shù)點(diǎn)具有其他意義(底下會(huì)介紹)所以必須要使用轉(zhuǎn)義字符(\)來(lái)加以解除其特殊意義!

 

找出空白行:

[root@www ~]# grep -n '^$' regular_express.txt22:

因?yàn)橹挥行惺赘形?(^$),所以,這樣就可以找出空白行啦!

 
5)

任意一個(gè)字節(jié) . 與重復(fù)字節(jié) *
這兩個(gè)符號(hào)在正則表達(dá)式的意義如下:

. (小數(shù)點(diǎn)):代表『一定有一個(gè)任意字節(jié)』的意思;* (星號(hào)):代表『重復(fù)前一個(gè)字符, 0 到無(wú)窮多次』的意思,為組合形態(tài)

假設(shè)我需要找出 g??d 的字串,亦即共有四個(gè)字節(jié), 起頭是 g 而結(jié)束是 d ,我可以這樣做:

[root@www ~]# grep -n 'g..d' regular_express.txt1:"Open Source" is a good mechanism to develop programs.9:Oh! The soup taste good.16:The world <Happy> is the same with "glad".

因?yàn)閺?qiáng)調(diào) g 與 d 之間一定要存在兩個(gè)字節(jié),因此,第 13 行的 god 與第 14 行的 gd 就不會(huì)被列出來(lái)啦!

 

如果我想要列出有 oo, ooo, oooo 等等的數(shù)據(jù), 也就是說(shuō),至少要有兩個(gè)(含) o 以上,該如何是好?

因?yàn)?* 代表的是『重復(fù) 0 個(gè)或多個(gè)前面的 RE 字符』的意義, 因此,『o*』代表的是:『擁有空字節(jié)或一個(gè) o 以上的字節(jié)』,
因此,『 grep -n 'o*' regular_express.txt 』將會(huì)把所有的數(shù)據(jù)都列印出來(lái)終端上!

當(dāng)我們需要『至少兩個(gè) o 以上的字串』時(shí),就需要 ooo* ,亦即是:

linux grep命令詳解
[root@www ~]# grep -n 'ooo*' 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. 9:Oh! The soup taste good. 18:google is the best tools for search keyword. 19:goooooogle yes!
linux grep命令詳解

 

如果我想要字串開頭與結(jié)尾都是 g,但是兩個(gè) g 之間僅能存在至少一個(gè) o ,亦即是 gog, goog, gooog.... 等等,那該如何?

[root@www ~]# grep -n 'goo*g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes!

 

如果我想要找出 g 開頭與 g 結(jié)尾的行,當(dāng)中的字符可有可無(wú)

[root@www ~]# grep -n 'g.*g' regular_express.txt1:"Open Source" is a good mechanism to develop programs.14:The gd software is a library for drafting programs.18:google is the best tools for search keyword. 19:goooooogle yes!  20:go! go! Let's go.


因?yàn)槭谴?g 開頭與 g 結(jié)尾,中間任意字節(jié)均可接受,所以,第 1, 14, 20 行是可接受的喔! 這個(gè).* 的 RE 表示任意字符是很常見的.

 

如果我想要找出『任意數(shù)字』的行?因?yàn)閮H有數(shù)字,所以就成為

[root@www ~]# grep -n '[0-9][0-9]*' regular_express.txt5:However, this dress is about $ 3183 dollars.15:You are the best is mean you are the no. 1.

 

 

限定連續(xù) RE 字符范圍 {}
我們可以利用 . 與 RE 字符及 * 來(lái)配置 0 個(gè)到無(wú)限多個(gè)重復(fù)字節(jié), 那如果我想要限制一個(gè)范圍區(qū)間內(nèi)的重復(fù)字節(jié)數(shù)呢?

舉例來(lái)說(shuō),我想要找出兩個(gè)到五個(gè) o 的連續(xù)字串,該如何作?這時(shí)候就得要使用到限定范圍的字符 {} 了。 但因?yàn)?{ 與 } 的符號(hào)在 shell 是有特殊意義的,因此, 我們必須要使用字符   \ 來(lái)讓他失去特殊意義才行。 至於 {} 的語(yǔ)法是這樣的,假設(shè)我要找到兩個(gè) o 的字串,可以是:

linux grep命令詳解
[root@www ~]# grep -n 'o\{2\}' regular_express.txt1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.9:Oh! The soup taste good.18:google is the best tools for search ke19:goooooogle yes!
linux grep命令詳解

 

假設(shè)我們要找出 g 后面接 2 到 5 個(gè) o ,然后再接一個(gè) g 的字串,他會(huì)是這樣:

[root@www ~]# grep -n 'go\{2,5\}g' regular_express.txt 18:google is the best tools for search keyword.

 

如果我想要的是 2 個(gè) o 以上的 goooo....g 呢?除了可以是 gooo*g ,也可以是:

[root@www ~]# grep -n 'go\{2,\}g' regular_express.txt18:google is the best tools for search keyword. 19:goooooogle yes!

 

擴(kuò)展grep(grep -E 或者 egrep):
使用擴(kuò)展grep的主要好處是增加了額外的正則表達(dá)式元字符集。

 

打印所有包含NW或EA的行。如果不是使用egrep,而是grep,將不會(huì)有結(jié)果查出。

    # egrep 'NW|EA' testfile     
    northwest       NW      Charles Main3.0 .98 3 34 eastern         EA      TB Savage4.4 .84 5 20

 

對(duì)于標(biāo)準(zhǔn)grep,如果在擴(kuò)展元字符前面加\,grep會(huì)自動(dòng)啟用擴(kuò)展選項(xiàng)-E。

#grep 'NW\|EA' testfile
northwest       NW      Charles Main3.0 .98 3 34 eastern         EA      TB Savage4.4 .84 5 20

 

搜索所有包含一個(gè)或多個(gè)3的行。

linux grep命令詳解
# egrep '3+' testfile
#grep -E '3+' testfile
#grep '3\+' testfile        
#這3條命令將會(huì)
northwest       NW      Charles Main3.0 .98 3 34 western         WE      Sharon Gray5.3 .97 5 23 northeast       NE      AM Main Jr.5.1 .94 3 13 central         CT      Ann Stephens5.7 .94 5 13
linux grep命令詳解

 

搜索所有包含0個(gè)或1個(gè)小數(shù)點(diǎn)字符的行。
    

linux grep命令詳解
# egrep '2\.?[0-9]' testfile 
#grep -E '2\.?[0-9]' testfile
#grep '2\.\?[0-9]' testfile 
#首先含有2字符,其后緊跟著0個(gè)或1個(gè)點(diǎn),后面再是0和9之間的數(shù)字。
western         WE       Sharon Gray5.3 .97 5 23 southwest       SW      Lewis Dalsass2.7 .8 2 18 eastern         EA       TB Savage4.4 .84 5 20
linux grep命令詳解

 

搜索一個(gè)或者多個(gè)連續(xù)的no的行。
    

# egrep '(no)+' testfile
#grep -E '(no)+' testfile
#grep '\(no\)\+' testfile   #3個(gè)命令返回相同結(jié)果,
northwest       NW      Charles Main3.0 .98 3 34 northeast       NE       AM Main Jr.5.1 .94 3 13 north           NO      Margot Weber4.5 .89 5 9


二:

不使用正則表達(dá)式

fgrep 查詢速度比grep命令快,但是不夠靈活:它只能找固定的文本,而不是規(guī)則表達(dá)式。

如果你想在一個(gè)文件或者輸出中找到包含星號(hào)字符的行

fgrep  '*' /etc/profile
for i in /etc/profile.d/*.sh ; do

或
grep -F '*' /etc/profile
for i in /etc/profile.d/*.sh ; do

分享文章:linuxgrep命令詳解
文章起源:http://muchs.cn/article40/pdgseo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、定制開發(fā)、靜態(tài)網(wǎng)站、App設(shè)計(jì)商城網(wǎng)站、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)