2013年3月8日 星期五 晴 有風
正則表達式練習
"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 <Happy> 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
1、過濾下載文件中包含 the 關鍵字
2、過濾下載文件中丌包含 the 關鍵字
3、過濾下載文件中丌論大小寫 the 關鍵字
4、過濾 test 或 taste 這兩個單字
5、過濾有 oo 的字節
6、過濾丌想要 oo 前面有 g 的
7、過濾 oo 前面丌想有小寫字節
8、過濾有數字的那一行
9、過濾以 the 開頭的
10、過濾以小寫字母開頭的
11、過濾開頭丌是英文字母
12、過濾行尾結束爲小數點.那一行
13、過濾空白行
14、過濾出 g??d 的字串
15、過濾至少兩個 o 以上的字串
16、過濾 g 開頭和 g 結尾但是兩個 g 之間僅存在至少一個 o
17、過濾任意數字的行
18、過濾兩個 o 的字串
19、過濾 g 後面接 2 到 5 個 o,然後在接一個 g 的字串
20、過濾 g 後面接 2 個以上 o 的
所給答案
[root@desktop1 ~]# grep -n 'the' regular_express.txt
[root@desktop1 ~]# grep -vn 'the' regular_express.txt
[root@desktop1 ~]# grep -in 'the' regular_express.txt
[root@desktop1 ~]# grep -n 't[ae]st' regular_express.txt
[root@desktop1 ~]# grep -n 'oo' regular_express.txt
[root@desktop1 ~]# grep -n '[^g]oo' regular_express.txt
[root@desktop1 ~]# grep -n '[^a-z]oo' regular_express.txt
[root@desktop1 ~]# grep -n '[^[:lower:]]oo' regular_express.txt
[root@desktop1 ~]# grep -n '[0-9]' regular_express.txt
[root@desktop1 ~]# grep -n '[[:digit:]]' regular_express.txt
[root@desktop1 ~]# grep -n '^the' regular_express.txt
[root@desktop1 ~]# grep -n '^[a-z]' regular_express.txt
[root@desktop1 ~]# grep -n '^[[:lower:]]' regular_express.txt
[root@desktop1 ~]# grep -n '^[^a-zA-Z]' regular_express.txt
[root@desktop1 ~]# grep -n '^[^[:alpha:]]' regular_express.txt
[root@desktop1 ~]# grep -n '\.$' regular_express.txt
[root@desktop1 ~]# grep -n '^$' regular_express.txt
[root@desktop1 ~]# grep -n 'g..d' regular_express.txt
[root@desktop1 ~]# grep -n 'ooo*' regular_express.txt
[root@desktop1 ~]# grep -n 'goo*g' regular_express.txt
[root@desktop1 ~]# grep -n 'goo*g' regular_express.txt
[root@desktop1 ~]# grep -n '[0-9][0-9]*' regular_express.txt
[root@desktop1 ~]# grep -n 'o\{2\}' regular_express.txt
[root@desktop1 ~]# grep -n 'go\{2,5\}g' regular_express.txt
[root@desktop1 ~]# grep -n 'go\{2,\}g' regular_express.txt
練習情況
1、過濾下載文件中包含 the 關鍵字
[root@desktop7 Desktop]# grep -n "the" regular_express.txt --color
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.
[root@desktop7 Desktop]#
2、過濾下載文件中不包含 the 關鍵字
[root@desktop7 Desktop]# grep -vn "the" regular_express.txt --color
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.
5:However, this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
7:Her hair is very beauty.
9:Oh! The soup taste good.
10:motorcycle is cheap than car.
11:This window is clear.
13:Oh! My god!
14:The gd software is a library for drafting programs.
17:I like dog.
19:goooooogle yes!
20:go! go! Let's go.
21:# I am VBird
22:
[root@desktop7 Desktop]#
3、過濾下載文件中不論大小寫 the 關鍵字
[root@desktop7 Desktop]# grep -in "the" regular_express.txt --color
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.
[root@desktop7 Desktop]#
4、過濾 test 或 taste 這兩個單字
[root@desktop7 Desktop]# grep -n "t[ea]st" regular_express.txt --color
8:I can't finish the test.
9:Oh! The soup taste good.
[root@desktop7 Desktop]#
5、過濾有 oo 的字節
[root@desktop7 Desktop]# grep -n "ooo*" regular_express.txt --color
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!
[root@desktop7 Desktop]#
6、過濾不想要 oo 前面有 g 的
[root@desktop7 Desktop]# grep -n '[^g]oo' regular_express.txt --color
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!
[root@desktop7 Desktop]#
7、過濾 oo 前面不想有小寫字節
[root@desktop7 Desktop]# grep -n '[^[:lower:]]oo' regular_express.txt --color
3:Football game is not use feet only.
過濾 oo 前面不想有數字
[root@desktop7 Desktop]# grep -n '[^[:digit:]]oo' regular_express.txt --color
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!
[root@desktop7 Desktop]#
8、過濾有數字的那一行
[root@desktop7 Desktop]# grep -n "[[:digit:]]" regular_express.txt --color
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.
[root@desktop7 Desktop]#
9、過濾以 the 開頭的
[root@desktop7 Desktop]# grep -n "^the" regular_express.txt --color 12:the symbol '*' is represented as start.
[root@desktop7 Desktop]#
10、過濾以小寫字母開頭的
[root@desktop7 Desktop]# grep -n "^[[:lower:]]" regular_express.txt --color
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@desktop7 Desktop]#
11、過濾開頭不是英文字母
[root@desktop7 Desktop]# grep -vn "^[[:alpha:]]" regular_express.txt --color
1:"Open Source" is a good mechanism to develop programs.
21:# I am VBird
22:
檢驗:反向查找
[root@desktop7 Desktop]# grep -n "^[[:alpha:]]" regular_express.txt --color
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
5:However, this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
7:Her hair is very beauty.
8:I can't finish the test.
9:Oh! The soup taste good.
10:motorcycle is cheap than car.
11:This window is clear.
12:the symbol '*' is represented as start.
13:Oh! My god!
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".
17:I like dog.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
[root@desktop7 Desktop]#
12、過濾行尾結束爲小數點.那一行
[root@desktop7 Desktop]# dos2unix regular_express.txt
dos2unix: converting file regular_express.txt to UNIX format ...
[root@desktop7 Desktop]# grep -n '\.$' regular_express.txt --color 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.
5:However, this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
7:Her hair is very beauty.
8:I can't finish the test.
9:Oh! The soup taste good.
10:motorcycle is cheap than car.
11:This window is clear.
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".
17:I like dog.
18:google is the best tools for search keyword.
20:go! go! Let's go.
[root@desktop7 Desktop]#
13、過濾空白行
[root@desktop7 Desktop]# dos2unix regular_express.txt
dos2unix: converting file regular_express.txt to UNIX format ...
[root@desktop7 Desktop]# grep -n '^$' regular_express.txt --color
22:
[root@desktop7 Desktop]#
14、過濾出 g??d 的字串
[root@desktop7 Desktop]# grep -n 'g??d' regular_express.txt --color
無
[root@desktop7 Desktop]# grep -n 'g..d' regular_express.txt --color
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world <Happy> is the same with "glad".
[root@desktop7 Desktop]#
15、過濾至少兩個 o 以上的字串
第一種方式
[root@desktop7 Desktop]# grep -n 'ooo*' regular_express.txt --color
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!
[root@desktop7 Desktop]#
第二種方式
[root@desktop7 Desktop]# grep -n 'o\{2,\}' regular_express.txt --color
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!
[root@desktop7 Desktop]#
16、過濾 g 開頭和 g 結尾但是兩個 g 之間僅存在至少一個 o
[root@desktop7 Desktop]# grep -n 'goo*g' regular_express.txt --color
18:google is the best tools for search keyword.
19:goooooogle yes!
[root@desktop7 Desktop]#
17、過濾任意數字的行
[root@desktop7 Desktop]# grep -n '[[:digit:]]' regular_express.txt --color
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.
[root@desktop7 Desktop]#
18、過濾兩個 o 的字串
方法一:
[root@desktop7 Desktop]# grep -n 'oo' regular_express.txt --color
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!
[root@desktop7 Desktop]#
方法二:
[root@desktop7 Desktop]# grep -n 'o\{2\}' regular_express.txt --color
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!
[root@desktop7 Desktop]#
19、過濾 g 後面接 2 到 5 個 o,然後再接一個 g 的字串
[root@desktop7 Desktop]# grep -n 'go\{2,5\}g' regular_express.txt --color
18:google is the best tools for search keyword.
[root@desktop7 Desktop]#
20、過濾 g 後面接 2 個以上 o 的
方法一:
[root@desktop7 Desktop]# grep -n 'go\{2,\}' regular_express.txt --color
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!
方法二:
[root@desktop7 Desktop]# grep -n 'gooo*' regular_express.txt --color
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!
[root@desktop7 Desktop]#