|
|
既然要使用 grep 當(dāng)然就得要先了解一下 grep 的語法啰~
[root@test root]# grep [-acinv] '搜尋字符串' filename
參數(shù)說明:
-a :將 binary 檔案以 text 檔案的方式搜尋數(shù)據(jù)
-c :計(jì)算找到 '搜尋字符串' 的次數(shù)
-i :忽略大小寫的不同,所以大小寫視為相同
-n :順便輸出行號(hào)
-v :反向選擇,亦即顯示出沒有 '搜尋字符串' 內(nèi)容的那一行!
范例:
[root@test root]# grep 'root' /var/log/secure
將 /var/log/secure 這個(gè)檔案中有 root 的那一行秀出來
[root@test root]# grep -v 'root' /var/log/secure
若該行沒有 root 才將數(shù)據(jù)秀出來到屏幕上!
[root@test root]# last | grep root
若該行有 root 才將數(shù)據(jù)秀出來到屏幕上! |
grep 是一個(gè)很常見也很常用的指令,他最重要的功能就是進(jìn)行字符串?dāng)?shù)據(jù)的比對(duì), 然后將符合使用者需求的字符串打印出來。 需要說明的是『grep 在資料中查尋一個(gè)字符串時(shí),是以 "整行" 為單位來進(jìn)行數(shù)據(jù)的擷取的!』也就是說,假如一個(gè)檔案內(nèi)有 10 行,其中有兩行具有你所搜尋的字符串,則將那兩行顯示在屏幕上,其它的就丟棄了!
而 grep 除了可以進(jìn)行檔案的資料搜尋之外,也常常被應(yīng)用在 input/output 的數(shù)據(jù)處理當(dāng)中,例如常見的 管線命令 ( pipe ) 就可以常常見到他的蹤影! 以上面表格中的例子來看,我們可以發(fā)現(xiàn)前兩個(gè)例子是查尋檔案的內(nèi)容,有沒有加上 -v 所顯示出來的結(jié)果是『相反的!』,而第三個(gè)例子則是以 pipe 的功能進(jìn)行數(shù)據(jù)的處理的喔!
好了,我們就開始以 grep 來進(jìn)行正規(guī)表示法的簡(jiǎn)易說明吧!我們先以底下這個(gè)檔案來作為范例:
[root@test root]# vi regular_express.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can’t finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh! My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
# I am VBird
|
需要特別注意的是,上面這個(gè)檔案鳥哥是在 Windows 的環(huán)境下編輯的, 并且經(jīng)過特殊處理過,因此,他雖然是純文字文件,但是內(nèi)含一些 Windows 環(huán)境下的軟件常常自行加入的一些特殊字符,例如斷行字符(^M)就是一例! 所以,您可以直接將上面的文字以 vi 儲(chǔ)存成 regular_express.txt 這個(gè)檔案, 不過,比較建議直接點(diǎn)底下的連結(jié)下載:
此外,因?yàn)椴煌恼Z系編碼是不一樣的,所以,您必須要將語系改成英文語系, 才能夠進(jìn)行底下的測(cè)試,否則,可能會(huì)有顯示的內(nèi)容與底下的輸出不符的狀況喔! 修改語系的方法為:
[root@test root]# LANG=en
[root@test root]# export LANG | 好了,現(xiàn)在開始我們一個(gè)案例一個(gè)案例的來介紹吧!
- 例題一、搜尋特定字符串:
搜尋特定字符串很簡(jiǎn)單吧?假設(shè)我們要從剛剛的檔案當(dāng)中取得 the 這個(gè)特定字符串, 最簡(jiǎn)單的方式就是這樣:
[root@test root]# grep -n 'the' regular_express.txt
8:I can't finish the test.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world is the same with "glad".
18:google is the best tools for search keyword.
| 那如果想要『反向選擇』呢?也就是說,當(dāng)該行沒有 'the' 這個(gè)字符串時(shí),才顯示在屏幕上,那就直接使用:
| [root@test root]# grep -vn 'the' regular_express.txt | 您會(huì)發(fā)現(xiàn),屏幕上出現(xiàn)的行列為除了 8,12,15,16,18 五行之外的其它行列! 接下來,如果您想要取得不論大小寫的 the 這個(gè)字符串,則:
[root@test root]# grep -in 'the' regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.
12:the symbol '*' is represented as start.
14:The gd software is a library for drafting programs.
15:You are the best is mean you are the no. 1.
16:The world is the same with "glad".
18:google is the best tools for search keyword.
|
- 例題二、利用 [] 來搜尋集合字符
如果我想要搜尋 test 或 taste 這兩個(gè)單字時(shí),可以發(fā)現(xiàn)到,其實(shí)她們有共通的 't?st' 存在~這個(gè)時(shí)候,我可以這樣來搜尋:
[root@test root]# grep -n 't[ae]st' regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good. | 了解了吧?其實(shí) [] 里面不論有幾個(gè)字符,他都謹(jǐn)代表某『一個(gè)』字符, 所以,上面的例子說明了,我需要的字符串是『tast』或『test』兩個(gè)字符串而已! 而如果想要搜尋到有 oo 的字符時(shí),則使用:
[root@test root]# grep -n 'oo' 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! | 但是,如果我不想要 oo 前面有 g 的話呢?此時(shí),可以利用在集合字符的反向選擇 [^] 來達(dá)成
[root@test root]# 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! | 意思就是說,我需要的是 oo ,但是 oo 前面不能是 g 就是了! 仔細(xì)比較上面兩個(gè)表格,妳會(huì)發(fā)現(xiàn),第 1,9 行不見了,因?yàn)?oo 前面出現(xiàn)了 g 所致! 第 2,3 行沒有疑問,因?yàn)?foo 與 Foo 均可被接受!但是第 18 行明明有 google 的 goo 啊~ 別忘記了,因?yàn)樵撔泻竺娉霈F(xiàn)了 tool 的 too 啊!所以該行也被列出來~ 也就是說, 18 行里面雖然出現(xiàn)了我們所不要的項(xiàng)目 (goo) 但是由于有需要的項(xiàng)目 (too) , 因此,是符合字符串搜尋的喔!
至于第 19 行,同樣的,因?yàn)?goooooogle 里面的 oo 前面可能是 o ,例如: go(ooo)oogle ,所以,這一行也是符合需求的!
再來,假設(shè)我 oo 前面不想要有小寫字符,所以,我可以這樣寫 [^abcd....z]oo , 但是這樣似乎不怎么方便,由于小寫字符的 ASCII 上編碼的順序是連續(xù)的, 因此,我們可以將之簡(jiǎn)化為底下這樣:
[root@test root]# grep -n '[^a-z]oo' regular_express.txt
3:Football game is not use feet only. | 也就是說,當(dāng)我們?cè)谝唤M集合字符中,如果該字符組是連續(xù)的,例如大寫英文/小寫英文/數(shù)字等等, 就可以使用[a-z],[A-Z],[0-9]等方式來書寫,那么如果我們的要求字符串是數(shù)字與英文呢? 呵呵!就將他全部寫在一起,變成:[a-zA-Z0-9]
例如,我們要取得有數(shù)字的那一行,就這樣:
[root@test root]# grep -n '[0-9]' regular_express.txt
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1. | 這樣對(duì)于 [] 以及 [^] 以及 [] 當(dāng)中的 - 有了解了嗎?! ^_^y - 例題三、行首與行尾字符 ^ $:
我們?cè)诶}一當(dāng)中,可以查詢到一行字符串里面有 the 的,那如果我想要讓 the 只在行首列出呢? 這個(gè)時(shí)候就得要使用定位字符了!我們可以這樣做:
[root@test root]# grep -n '^the' regular_express.txt
12:the symbol '*' is represented as start. | 此時(shí),就只剩下第 12 行,因?yàn)橹挥械?12 行的行首是 the 開頭啊~此外, 如果我想要開頭是小寫字符的那一行就列出呢?可以這樣:
[root@test root]# 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 start.
18:google is the best tools for search keyword.
19:goooooogle yes! | 如果我不想要開頭是英文字母,則可以是這樣:
[root@test root]# grep -n '^[^a-zA-Z]' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
20:# I am VBird | 注意到了吧?那個(gè) ^ 符號(hào),在字符集合符號(hào)(括號(hào)[])之內(nèi)與之外是不同的! 在 [] 內(nèi)代表『反向選擇』,在 [] 之外則代表定位在行首的意義!要分清楚喔!
那如果我想要找出來,行尾結(jié)束為小數(shù)點(diǎn) (.) 的那一行,該如何處理:
[root@test root]# 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 is the same with "glad".
17:I like dog.
18:google is the best tools for search keyword.
| 特別注意到,因?yàn)樾?shù)點(diǎn)具有其它意義(底下會(huì)介紹),所以必須要使用跳脫字符(\)來加以解除其特殊意義! 不過,您或許會(huì)覺得奇怪,但是第 5~9 行最后面也是 . 啊~怎么無法打印出來?? 這里就牽涉到 Windows 平臺(tái)的軟件對(duì)于斷行字符的判斷問題了!我們使用 cat -A 將第五行拿出來看, 您會(huì)發(fā)現(xiàn):
[root@test root]# cat -A regular_express.txt
However, this dress is about $ 3183 dollars.^M$ | 注意到了沒?最后面的斷行字符應(yīng)該是 $ 才對(duì),但是,因?yàn)?Windows 的 nodepad 會(huì)主動(dòng)加上 ^M 作為斷行的判斷,因此,那個(gè) . 自然就不是緊接在 $ 之前喔!這樣可以了解 ^ 與 $ 的意義嗎? 好了,先不要看底下的解答,自己想一想,那么如果我想要找出來,哪一行是『空白行』, 也就是說,該行并沒有輸入任何數(shù)據(jù),該如何搜尋??
[root@test root]# grep -n '^$' regular_express.txt
21: | 因?yàn)橹挥行惺赘形? ^$ ),所以,這樣就可以找出空白行啦!再來, 假設(shè)您已經(jīng)知道在一個(gè)批次腳本 (shell script) 或者是設(shè)定檔當(dāng)中, 空白行與開頭為 # 的那一行是批注,因此如果您要將數(shù)據(jù)列出給別人參考時(shí), 可以將這些數(shù)據(jù)省略掉,以節(jié)省保貴的紙張,那么,您可以怎么作呢? 我們以 /etc/syslog.conf 這個(gè)檔案來作范例,您可以自行參考一下輸出的結(jié)果:
[root@test root]# cat /etc/syslog.conf
[root@test root]# grep -v '^$' /etc/syslog.conf | grep -v '^#' | 是否節(jié)省很多版面啊?? - 例題四、任意一個(gè)字符 . 與重復(fù)字符 *
在 bash 的章節(jié)當(dāng)中,我們知道萬用字符 * 可以用來代表任意(0或多個(gè))字符, 但是正規(guī)表示法并不是萬用字符,兩者之間是不相同的! 至于正規(guī)表示法當(dāng)中的『 . 』則代表『絕對(duì)有一個(gè)任意字符』的意思!這樣講不好懂, 我們直接做個(gè)練習(xí)吧!假設(shè)我需要找出 g??d 的字符串,亦即共有四個(gè)字符, 起頭是 g 而結(jié)束是 d ,我可以這樣做:
[root@test root]# grep -n 'g..d' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world is the same with "glad".
| 因?yàn)閺?qiáng)調(diào) g 與 d 之間一定要存在兩個(gè)字符,因此,第 13 行的 god 與第 14 行的 gd 就不會(huì)被列出來啦!再來,如果我想要列出有 oo, ooo, oooo 等等的資料, 也就是說,至少要有兩個(gè) o 以上,該如何是好??是 o* 還是 oo* 還是 ooo* 呢? 雖然您可以試看看結(jié)果, 不過結(jié)果太占版面了 @_@ ,所以,我這里就直接說明。
因?yàn)?* 代表的是『重復(fù) 0 個(gè)或多個(gè)前面的 RE 字符』的意義, 因此,『o*』代表的是:『擁有空字符或一個(gè) o 以上的字符』, 特別注意,因?yàn)樵试S空字符(就是有沒有字符都可以的意思),因此, grep -n 'o*' regular_express.txt 將會(huì)把所有的數(shù)據(jù)都打印出來屏幕上!
那如果是『oo*』呢?則第一個(gè) o 肯定必須要存在,第二個(gè) o 則是可有可無的多個(gè) o , 所以,凡是含有 o, oo, ooo, oooo 等等,都可以被列出來~
同理,當(dāng)我們需要『至少兩個(gè) o 以上的字符串』時(shí),就需要 ooo* ,亦即是:
[root@test root]# 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! | 這樣理解 * 的意義了嗎?!好了,現(xiàn)在出個(gè)練習(xí),如果我想要字符串開頭與結(jié)尾都是 g, 但是兩個(gè) g 之間僅能存在至少一個(gè) o ,亦即是 gog, goog, gooog.... 等等, 那該如何?
[root@test root]# grep -n 'goo*g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes! | 如此了解了嗎?好,再來一題,如果我想要找出 g 開頭與 g 結(jié)尾的字符串, 當(dāng)中的字符可有可無,那該如何是好?是『g*g』嗎?
[root@test root]# grep -n 'g*g' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
9:Oh! The soup taste good.
13:Oh! My god!
14:The gd software is a library for drafting programs.
16:The world is the same with "glad".
17:I like dog.
18:google is the best tools for search keyword.
19:goooooogle yes!
| 但測(cè)試的結(jié)果竟然出現(xiàn)這么多行??太詭異了吧? 其實(shí)一點(diǎn)也不詭異,因?yàn)?g*g 里面的 g* 代表『空字符或一個(gè)以上的 g』 在加上后面的 g ,因此,整個(gè) RE 的內(nèi)容就是 g, gg, ggg, gggg , 因此,只要該行當(dāng)中擁有一個(gè)以上的 g 就符合所需了!
那該如何得到我們的 g....g 的需求呢?呵呵!就利用任意一個(gè)字符『.』啊! 亦即是:『g.*g』的作法,因?yàn)?* 可以是 0 或多個(gè)重復(fù)前面的字符,而 . 是任意字符,所以: 『.* 就代表零個(gè)或多個(gè)任意字符』的意思啦!
[root@test root]# grep -n 'g.*g' regular_express.txt
1:"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! | 因?yàn)槭谴?g 開頭與 g 結(jié)尾,中間任意字符均可接受,所以,第 1 與第 14 行是可接受的喔! 這個(gè) .* 的 RE 表示任意字符是很常見的,希望大家能夠理解并且熟悉!
再出一題,如果我想要找出『任意數(shù)字』的行列呢?因?yàn)閮H有數(shù)字,所以就成為:
[root@test root]# grep -n '[0-9][0-9]*' regular_express.txt
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1. | 雖然使用 grep -n '[0-9]' regular_express.txt 也可以得到相同的結(jié)果, 但鳥哥希望大家能夠理解上面指令當(dāng)中 RE 表示法的意義才好! - 例題五、限定連續(xù) RE 字符范圍 {}
在上個(gè)例題當(dāng)中,我們可以利用 . 與 RE 字符及 * 來設(shè)定 0 個(gè)到無線多個(gè)重復(fù)字符, 那如果我想要限制一個(gè)范圍區(qū)間內(nèi)的重復(fù)字符數(shù)呢?舉例來說,我想要找出兩個(gè)到五個(gè) o 的連續(xù)字符串,該如何作?這時(shí)候就得要使用到限定范圍的字符 {} 了。 但因?yàn)?{ 與 } 的符號(hào)在 shell 是有特殊意義的,因此, 我們必須要使用跳脫字符 \ 來讓他失去特殊意義才行。
至于 {} 的語法是這樣的,假設(shè)我要找到兩個(gè) o 的字符串,可以是:
[root@test root]# grep -n 'o\{2\}' 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! | 這樣看似乎與 ooo* 的字符沒有什么差異啊?因?yàn)榈?19 行有多個(gè) o 依舊也出現(xiàn)了! 好,那么換個(gè)搜尋的字符串,假設(shè)我們要找出 g 后面接 2 到 5 個(gè) o ,然后再接一個(gè) g 的字符串, 他會(huì)是這樣:
[root@test root]# grep -n 'go\{2,5\}g' regular_express.txt
18:google is the best tools for search keyword. | 嗯!很好!第 19 行終于沒有被取用了(因?yàn)?19 行有 6 個(gè) o 啊!)。 那么,如果我想要的是 2 個(gè) o 以上的 goooo....g 呢?除了可以是 gooo*g ,也可以是:
[root@test root]# grep -n 'go\{2,\}g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes! | 呵呵!就可以找出來啦~
◎grep -- print lines matching a pattern
grep [options] PATTERN [FILE...]
grep [options] [-e PATTERN | -f FILE] [FILE...]
grep用以在file內(nèi)文中比對(duì)相對(duì)應(yīng)的部分,或是當(dāng)沒有指定檔案時(shí),由標(biāo)準(zhǔn)輸入中去比對(duì)。 在預(yù)設(shè)的情況下,grep會(huì)將符合樣式的那一行列出。
此外,還有兩個(gè)程式是grep的變化型,egrep及fgrep其中egrep就等同於grep -E ,fgrep等同於grep -F 。
◎參數(shù)
1. -A NUM,--after-context=NUM
除了列出符合行之外,并且列出后NUM行。
ex: $ grep -A 1 panda file
(從file中搜尋有panda樣式的行,并顯示該行的后1行)
2. -a或--text
grep原本是搜尋文字檔,若拿二進(jìn)位的檔案作為搜尋的目標(biāo),
則會(huì)顯示如下的訊息: Binary file 二進(jìn)位檔名 matches 然后結(jié)束。
若加上-a參數(shù)則可將二進(jìn)位檔案視為文字檔案搜尋,
相當(dāng)於--binary-files=text這個(gè)參數(shù)。
ex: (從二進(jìn)位檔案mv中去搜尋panda樣式)
(錯(cuò)誤!!!)
$ grep panda mv
Binary file mv matches
(這表示此檔案有match之處,詳見--binary-files=TYPE )
$ (正確!!!)
$ grep -a panda mv
3. -B NUM,--before-context=NUM
與 -A NUM 相對(duì),但這此參數(shù)是顯示除符合行之外
并顯示在它之前的NUM行。
ex: (從file中搜尋有panda樣式的行,并顯示該行的前1行)
$ grep -B 1 panda file
4. -C [NUM], -NUM, --context[=NUM]
列出符合行之外并列出上下各NUM行,預(yù)設(shè)值是2。
ex: (列出file中除包含panda樣式的行外并列出其上下2行)
(若要改變預(yù)設(shè)值,直接改變NUM即可)
$ grep -C[NUM] panda file
5. -b, --byte-offset
列出樣式之前的內(nèi)文總共有多少byte ..
ex: $ grep -b panda file
顯示結(jié)果類似於:
0:panda
66:pandahuang
123:panda03
6. --binary-files=TYPE
此參數(shù)TYPE預(yù)設(shè)為binary(二進(jìn)位),若以普通方式搜尋,只有2種結(jié)果:
1.若有符合的地方:顯示Binary file 二進(jìn)位檔名 matches
2.若沒有符合的地方:什麼都沒有顯示。
若TYPE為without-match,遇到此參數(shù),
grep會(huì)認(rèn)為此二進(jìn)位檔案沒有包含任何搜尋樣式,與-I 參數(shù)相同。
若TPYE為text, grep會(huì)將此二進(jìn)位檔視為text檔案,與-a 參數(shù)相同。
Warning: --binary-files=text 若輸出為終端機(jī),可能會(huì)產(chǎn)生一些不必要的輸出。
7. -c, --count
不顯示符合樣式行,只顯示符合的總行數(shù)。
若再加上-v,--invert-match,參數(shù)顯示不符合的總行數(shù)。
8. -d ACTION, --directories=ACTION
若輸入的檔案是一個(gè)資料夾,使用ACTION去處理這個(gè)資料夾。
預(yù)設(shè)ACTION是read(讀取),也就是說此資料夾會(huì)被視為一般的檔案;
若ACTION是skip(略過),資料夾會(huì)被grep略過:
若ACTION是recurse(遞),grep會(huì)去讀取資料夾下所有的檔案,
此相當(dāng)於-r 參數(shù)。
9. -E, --extended-regexp
采用規(guī)則表示式去解釋樣式。
10. -e PATTERN, --regexp=PATTERN
把樣式做為一個(gè)partern,通常用在避免partern用-開始。
11. -f FILE, --file=FILE
事先將要搜尋的樣式寫入到一個(gè)檔案,一行一個(gè)樣式。
然后采用檔案搜尋。
空的檔案表示沒有要搜尋的樣式,因此也就不會(huì)有任何符合。
ex: (newfile為搜尋樣式檔)
$grep -f newfile file
12. -G, --basic-regexp
將樣式視為基本的規(guī)則表示式解釋。(此為預(yù)設(shè))
13. -H, --with-filename
在每個(gè)符合樣式行前加上符合的檔案名稱,若有路徑會(huì)顯示路徑。
ex: (在file與testfile中搜尋panda樣式)
$grep -H panda file ./testfile
file:panda
./testfile:panda
$
14. -h, --no-filename
與-H參數(shù)相類似,但在輸出時(shí)不顯示路徑。
15. --help
產(chǎn)生簡(jiǎn)短的help訊息。
16. -I
grep會(huì)強(qiáng)制認(rèn)為此二進(jìn)位檔案沒有包含任何搜尋樣式,
與--binary-files=without-match參數(shù)相同。
ex: $ grep -I panda mv
17. -i, --ignore-case
忽略大小寫,包含要搜尋的樣式及被搜尋的檔案。
ex: $ grep -i panda mv
18. -L, --files-without-match
不顯示平常一般的輸出結(jié)果,反而顯示出沒有符合的檔案名稱。
19. -l, --files-with-matches
不顯示平常一般的輸出結(jié)果,只顯示符合的檔案名稱。
20. --mmap
如果可能,使用mmap系統(tǒng)呼叫去讀取輸入,而不是預(yù)設(shè)的read系統(tǒng)呼叫。
在某些狀況,--mmap 能產(chǎn)生較好的效能。 然而,--mmap
如果運(yùn)作中檔案縮短,或I/O 錯(cuò)誤發(fā)生時(shí),
可能造成未定義的行為(包含core dump),。
21. -n, --line-number
在顯示行前,標(biāo)上行號(hào)。
ex: $ grep -n panda file
顯示結(jié)果相似於下:
行號(hào):符合行的內(nèi)容
22. -q, --quiet, --silent
不顯示任何的一般輸出。請(qǐng)參閱-s或--no-messages
23. -r, --recursive
遞地,讀取每個(gè)資料夾下的所有檔案,此相當(dāng)於 -d recsuse 參數(shù)。
24. -s, --no-messages
不顯示關(guān)於不存在或無法讀取的錯(cuò)誤訊息。
小: 不像GNU grep,傳統(tǒng)的grep不符合POSIX.2協(xié)定,
因?yàn)槿狈?q參數(shù),且他的-s 參數(shù)表現(xiàn)像GNU grep的 -q 參數(shù)。
Shell Script傾向?qū)鹘y(tǒng)的grep移植,避開-q及-s參數(shù),
且將輸出限制到/dev/null。
POSIX: 定義UNIX及UNIX-like系統(tǒng)需要提供的功能。
25. -V, --version
顯示出grep的版本號(hào)到標(biāo)準(zhǔn)錯(cuò)誤。
當(dāng)您在回報(bào)有關(guān)grep的bugs時(shí),grep版本號(hào)是必須要包含在內(nèi)的。
26. -v, --invert-match
顯示除搜尋樣式行之外的全部。
27. -w, --word-regexp
將搜尋樣式視為一個(gè)字去搜尋,完全符合該"字"的行才會(huì)被列出。
28. -x, --line-regexp
將搜尋樣式視為一行去搜尋,完全符合該"行"的行才會(huì)被列出。
|
|