1. 程式人生 > >linux 正則表示式之grep

linux 正則表示式之grep

正則表示式就是處理字串的方法,可以執行查詢,刪除,替換等特定字元處理程式。 常用命令 grep ,awk ,sed

[: alunm:] 代表英文大小寫字元及數字 即0-9 A-Z a-z [: alpha:] 代表任何英文大小寫字元 A-Z a-z [:blank :] 代表空格鍵與[Tab]按鍵 [: dight :] 代表0-9 [: lower:] 代表小寫字元 及a-z

grep 常用日常用法 -i 忽略大小寫 -n 輸出查詢的行號 -v 反向選擇 及顯示出沒有查詢字串內容那一行

"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.^M GNU is free air not free beer.^M Her hair is very beauty.^M I can't finish the test.^M Oh! The soup taste good.^M 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.^M 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.查詢特定字串

 [[email protected] 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.

忽略大小寫 +i 反向取+v 2.利用[ ] 來查詢集合字元 列如果想要查詢 test 和 taste

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

3.找出oo前面不要小寫字元

[root@localhost tmp]# grep -b '[^a-z]oo'  regular_express.txt
82:Football game is not use feet only.

4.找出以the開頭的行

[[email protected] tmp]# grep -n '^the' regular_express.txt
12:the symbol '*' is represented as start.

5找出以 . 結尾的行

[[email protected] 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.

此時的.需要\來轉義 6.查詢空白行

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

7.在正則表示式中 . (小數點) 代表一定有一個任意字元的意思 * (星號) 代表重複前一個0到無窮多次的意思,為組合形態 列如 找出g??d 字串

[[email protected] tmp]# 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 <Happy> is the same with "glad".

8.查詢兩個o以上的字串

[[email protected] 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!

9.查詢g……g 的字串

[[email protected] tmp]# grep -n 'g.*' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.

13:Oh!  My god!

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.

限制一個範圍區間匹配重複字元數

10.假設找到連續ooo 的字串

[[email protected] tmp]# 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!

11.匹配3-5個oo

[root@localhost tmp]# grep -n 'o\{3,5\}' regular_express.txt
19:goooooogle yes!