實例文件下載:linux
wget http://linux.vbird.org/linux_basic/0330regularex/regular_express.txt
express
例題一:查找特定字符app
[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.
例題二:反選ide
[root@mail_bk tmp]# grep -nv 'the' regular_express.txt
例題三:查找特定字符不論大小寫this
[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.
例題四:利用中括號[]來查找集合字符
google
一、查找test和taste這兩個單詞spa
[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.
或者字符串
[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.
注:其實[]裏面不論有幾個字符,他都表明「一個」字符get
例題五:查找不是以g開頭的oo字符it
[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!
注:你發現1八、19行仍是有g開頭,這是由於該行tool是被接受的
例題六:查找不是以字母開頭的oo字符
[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]
[root@mail_bk tmp]# grep -n '[a-zA-Z0-9]' regular_express.txt
例題七:查找以the開頭
[root@mail_bk tmp]# grep -n '^the' regular_express.txt 12:the symbol '*' is represented as start.
例題八:查找以小寫字母開頭
[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.
例題九:查找不是英文字母開頭
[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:
或者:
[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
注意:在中括號裏面和外面含義是不一樣的,在括號裏面標示反選,在括號外面表示行首
例題十:查找以.點結尾的
[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.
注意:由於小數點具備特殊含義,因此必須使用轉義符(\)
例題十一:查找空白行
[root@mail_bk tmp]# grep -n '^$' regular_express.txt 22:
例題十二:任意一個字符.點與任意重複字符*
.表明必定有一個任意字符的意思
*表明重複前面字符0到無窮個
一、查找g??d的字符
[root@mail_bk tmp]# grep -n 'g..g' regular_express.txt 18:google is the best tools for search keyword.
二、查找兩個o以上的字符串
[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!
三、查找gog,goog,gooog字符
[root@mail_bk tmp]# grep -n 'go*g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes!
四、查找g開頭與g結尾的字符串,當中字符串無關緊要
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.
五、查找任意數字的字符
[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的連續字符串
[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結尾
[root@mail_bk tmp]# grep -n 'go\{2,5\}g' regular_express.txt 18:google is the best tools for search keyword.