整合作品!
一、grep 參數使用
Gun grep 選項
-b 在搜索到的行的前面打印該行所在的塊號碼。
實例文件下載:
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 '*' 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" . 18 :google is the best tools for search 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 '*' 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 <Happy> is the same with "glad" . 18 :google is the best tools for search 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 is my favorite food. 3 :Football game is not use feet only. 18 :google is the best tools for search 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 is not use feet 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 '*' is represented as start. |
例題八:查找以小寫字母開頭
1 2 3 4 5 6 7 8 | [root@mail_bk 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 start. 18 :google is the best tools for search 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" is a 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" is a 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" 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. |
注意:因爲小數點具有特殊含義,所以必須使用轉義符(\)
例題十一:查找空白行
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 is the best tools for search keyword. |
2、查找兩個o以上的字符串
1 2 3 4 5 6 7 | [root@mail_bk tmp]# 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! |
3、查找gog,goog,gooog字符
1 2 3 | [root@mail_bk tmp]# grep -n 'go*g' regular_express.txt 18 :google is the best tools for search keyword. 19 :goooooogle yes! |
4、查找g開頭與g結尾的字符串,當中字符串可有可無
1 2 3 4 5 | 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! 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, this dress is about $ 3183 dollars. 15 :You are the best is mean 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" 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! |
例題十四:查找g開頭2-5個o的連續字符串,然後g結尾
1 2 | [root@mail_bk tmp]# grep -n 'go\{2,5\}g' regular_express.txt 18 :google is the best tools for search keyword. |