Grep應用

整合作品!

一、grep 參數使用

Gun grep 選項

-b 在搜索到的行的前面打印該行所在的塊號碼。

-c 只顯示有多少行匹配 ,而不具體顯示匹配的行
-h 不顯示文件名
-i 在字符串比較的時候忽略大小寫
-l 只顯示包含匹配模板的行的文件名清單,不同項目之間用換行符分隔
-L 打印不匹配模板的文件名清單
-n 在每一行前面打印該行在文件中的行數
-s 靜默工作,除非出現錯誤信息否則不打印任何信息,這個功能在檢測退出狀態的時候有用
-v 反檢索,只顯示不匹配的行
-w
-Ax 在匹配指定行打印完畢後,再打印x行(向原文件匹配行下x行)
-By 在匹配指定行前面打印y行(在原文件匹配行上面打印y行)
-Cz 在匹配行前後打印z行 (在原文件匹配行上下打印z行)
-b 在每一行前面打印字符偏移量
-f file 從文件file中提取模板。空文件中包含0個模板
-q 取消標準輸出,跟-n功能是一樣的
-s 不顯示關於不存在或者無法讀文件的錯誤信息
-w 只打印以單詞形式匹配模板的行,模板可以是包含數字、字符和下劃線的字符串
-x 只打印整行匹配的行
-y 用法同-i
-U 把文件作爲二進制文件,這個選項只在MS-DOS和MS-Windows中被支持(這個參數沒有明白,請過路高人指點,非常感謝)
-u 按照unix風格報告字符偏移量。只在-b選項同時被使用的時候纔有效。這個選項只在MS-DOS和MS-Windows中被支持
grep "$name" file 把變量$name 的值作爲模板,在文件中尋找匹配模板的行。注意,必須使用雙引號
重複作用的元字符, \{\}; 用來做標籤的元字符,\(\); 用來錨定單詞的元字符\<\>


實例文件下載:

wget http://linux.vbird.org/linux_basic/0330regularex/regular_express.txt

例題一:查找特定字符

1
2
3
4
5
6
[root@mail_bk tmp]# grep -n 'the'regular_express.txt
8:I can't finish the test.
12:the symbol '*'isrepresented asstart.
15:You are the best ismean you are the no. 1.
16:The world <Happy> isthe same with"glad".
18:google isthe best tools forsearch keyword.

例題二:反選

1
[root@mail_bk tmp]# grep -nv 'the'regular_express.txt

例題三:查找特定字符不論大小寫

1
2
3
4
5
6
7
8
[root@mail_bk tmp]# grep -ni 'the'regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.
12:the symbol '*'isrepresented asstart.
14:The gd software isa library fordrafting programs.
15:You are the best ismean you are the no. 1.
16:The world <Happy> isthe same with"glad".
18:google isthe best tools forsearch keyword.

例題四:利用中括號[]來查找集合字符

1、查找test和taste這兩個單詞

1
2
3
[root@mail_bk tmp]# grep -n 't[a-z]st'regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.

或者

1
2
3
[root@mail_bk tmp]# grep -n 't[ae]st'regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.


注:其實[]裏面不論有幾個字符,他都代表“一個”字符

例題五:查找不是以g開頭的oo字符

1
2
3
4
5
[root@mail_bk tmp]# grep -n '[^g]oo'regular_express.txt
2:apple ismy favorite food.
3:Football game isnot usefeet only.
18:google isthe best tools forsearch keyword.
19:goooooogle yes!

注:你發現18、19行還是有g開頭,這是因爲該行tool是被接受的

例題六:查找不是以字母開頭的oo字符

1
2
[root@mail_bk tmp]# grep -n '[^a-z]oo'regular_express.txt
3:Football game isnot usefeet only.

總結:我們可以使用[a-z][A-Z][0-9]等方式來寫,如果要求字符串是數字加字母,可以寫成[a-zA-Z0-9]

1
[root@mail_bk tmp]# grep -n '[a-zA-Z0-9]'regular_express.txt

例題七:查找以the開頭

1
2
[root@mail_bk tmp]# grep -n '^the'regular_express.txt
12:the symbol '*'isrepresented asstart.

例題八:查找以小寫字母開頭

1
2
3
4
5
6
7
8
[root@mail_bk tmp]# grep -n '^[a-z]'regular_express.txt
2:apple ismy favorite food.
4:thisdress doesn't fit me.
10:motorcycle ischeap than car.
12:the symbol '*'isrepresented asstart.
18:google isthe best tools forsearch keyword.
19:goooooogle yes!
20:go! go! Let's go.

例題九:查找不是英文字母開頭

1
2
3
4
[root@mail_bk tmp]# grep -nv '^[a-zA-Z]'regular_express.txt
1:"Open Source"isa good mechanism to develop programs.
21:# I am VBird
22:

或者:

1
2
3
[root@mail_bk tmp]# grep -n '^[^a-zA-Z]'regular_express.txt
1:"Open Source"isa good mechanism to develop programs.
21:# I am VBird

注意:在中括號裏面和外面含義是不同的,在括號裏面標示反選,在括號外面表示行首

例題十:查找以.點結尾的

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@mail_bk tmp]# grep -n '\.$'regular_express.txt
1:"Open Source"isa good mechanism to develop programs.
2:apple ismy favorite food.
3:Football game isnot usefeet only.
4:thisdress doesn't fit me.
10:motorcycle ischeap than car.
11:This window isclear.
12:the symbol '*'isrepresented asstart.
15:You are the best ismean you are the no. 1.
16:The world <Happy> isthe same with"glad".
17:I like dog.
18:google isthe best tools forsearch keyword.
20:go! go! Let's go.

注意:因爲小數點具有特殊含義,所以必須使用轉義符(\)

例題十一:查找空白行

1
2
[root@mail_bk tmp]# grep -n '^$'regular_express.txt
22:

例題十二:任意一個字符.點與任意重複字符*

.代表一定有一個任意字符的意思

*代表重複前面字符0到無窮個

1、查找g??d的字符

1
2
[root@mail_bk tmp]# grep -n 'g..g'regular_express.txt
18:google isthe best tools forsearch keyword.

2、查找兩個o以上的字符串

1
2
3
4
5
6
7
[root@mail_bk tmp]# grep -n 'ooo*'regular_express.txt
1:"Open Source"isa good mechanism to develop programs.
2:apple ismy favorite food.
3:Football game isnot usefeet only.
9:Oh! The soup taste good.
18:google isthe best tools forsearch keyword.
19:goooooogle yes!

3、查找gog,goog,gooog字符

1
2
3
[root@mail_bk tmp]# grep -n 'go*g'regular_express.txt
18:google isthe best tools forsearch keyword.
19:goooooogle yes!

4、查找g開頭與g結尾的字符串,當中字符串可有可無

1
2
3
4
5
1:"Open Source"isa good mechanism to develop programs.
14:The gd software isa library fordrafting programs.
18:google isthe best tools forsearch keyword.
19:goooooogle yes!
20:go! go! Let's go.

5、查找任意數字的字符

1
2
3
[root@mail_bk tmp]# grep -n '[0-9][0-9]*'regular_express.txt
5:However, thisdress isabout $ 3183dollars.
15:You are the best ismean you are the no. 1.

例題十三:查找2-5個o的連續字符串

1
2
3
4
5
6
7
[root@mail_bk tmp]# grep -n 'o\{2,5\}'regular_express.txt
1:"Open Source"isa good mechanism to develop programs.
2:apple ismy favorite food.
3:Football game isnot usefeet only.
9:Oh! The soup taste good.
18:google isthe best tools forsearch keyword.
19:goooooogle yes!

例題十四:查找g開頭2-5個o的連續字符串,然後g結尾

1
2
[root@mail_bk tmp]# grep -n 'go\{2,5\}g'regular_express.txt
18:google isthe best tools forsearch keyword.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章