1. 程式人生 > >Grep(egrep)與正則表示式

Grep(egrep)與正則表示式

如下,g??d 可以用 'g..d' 表示。 good ,gxxd ,gabd .....都符合。
[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 is the same with 'glad'.
[email protected]:~/tmp$

搜尋兩個o以上的字串
[email protected]:~/tmp$ grep -n 'ooo*' regular_express.txt //前兩個o一定存在,第三個o可沒有,也可有多個。
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開頭和結尾,中間是至少一個o的字串,即gog, goog....gooog...等
[email protected]
:~/tmp$ grep -n 'goo*g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!

搜尋g開頭和結尾的字串在的行
[email protected]:~/tmp$ grep -n 'g.*g' regular_express.txt     // .*表示 0個或多個任意字元
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.


9)限定連續重複字元的範圍 { }
. * 只能限制0個或多個, 如果要確切的限制字元重複數量,就用{範圍} 。範圍是數字用,隔開 2,5 表示2~5個,
2表示2個,2, 表示2到更多個
注意,由於{ }在SHELL中有特殊意義,因此作為正則表示式用的時候要用\轉義一下。

搜尋包含兩個o的字串的行。
[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!

搜尋g後面跟2~5個o,後面再跟一個g的字串的行。
[email protected]
:~/tmp$ grep -n 'go\{2,5\}g' regular_express.txt
18:google is the best tools for search keyword.


搜尋包含g後面跟2個以上o,後面再跟g的行。。
[email protected]:~/tmp$ grep -n 'go\{2,\}g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!


注意,相讓[]中的^ - 不表現特殊意義,可以放在[]裡面內容的後面。
'[^a-z\.!^ -]' 表示沒有小寫字母,沒有. 沒有!, 沒有空格,沒有- 的 串,注意[]裡面有個小空格。

另外shell 裡面的反向選擇為[!range], 正則裡面是 [^range]


擴充套件正則表示式

擴充套件正則表示式是對基礎正則表示式添加了幾個特殊構成的。
它令某些操作更加方便。
比如我們要去除 空白行和行首為 #的行, 會這樣用:
[email protected]:~/tmp$ grep -v '^$' regular_express.txt | grep -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.
............

然而使用支援擴充套件正則表示式的 egrep 與擴充套件特殊符號 | ,會方便許多。
注意grep只支援基礎表示式, 而egrep 支援擴充套件的, 其實 egrep 是 grep -E 的別名而已。因此grep -E 支援擴充套件正則。
那麼:
[email protected]:~/tmp$ egrep -v '^$|^#' regular_express.txt
"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.
....................
這裡| 表示或的關係。 即滿足 ^$ 或者 ^# 的字串。

這裡列出幾個擴充套件特殊符號:
+, 於 . * 作用類似,表示 一個或多個重複字元。
?, 於 . * 作用類似,表示0個或一個字元。
|,表示或關係,比如 'gd|good|dog' 表示有gd,good或dog的串
(),將部分內容合成一個單元組。 比如 要搜尋 glad 或 good 可以這樣 'g(la|oo)d'
()的好處是可以對小組使用 + ? * 等。
比如要搜尋A和C開頭結尾,中間有至少一個(xyz) 的串,可以這樣 : 'A(xyz)+C'
附註:egrep也很好用,剛剛在寫一個shell指令碼,正好用到了這個;蠻簡單的一個道理,有時候,你並不僅僅只grep -v 一個物件,如egrep -v "192.168.1.101|102|104"這種用法就蠻適用於egrep。