1.搜索有the的行,并輸出行號
#grep -n 'the' regular_express.txt
搜索沒有the的行,并輸出行號
#grep -nv 'the' regular_express.txt
2.利用[]搜索集合字符
[] 表示其中的某一個字符 只壳,例如[ade] 表示a或d或e
# grep -n 't[ae]st' regular_express.txt
8:I can't finish the test.
9:Oh! the soup taste good!
可以用^符號做[]內(nèi)的前綴映皆,表示除[]內(nèi)的字符之外的字 符。
比如搜索oo前沒有g(shù)的字符串所在的行. 使用 '[^g]oo' 作搜索字符串
# 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!
[] 內(nèi)可以用范圍表示,比如[a-z] 表示小寫字母,[0-9] 表示0~9的數(shù)字, [A-Z] 則是大寫字母們劣纲。[a-zA-Z0-9]
表示所有數(shù)字與英文字符。 當(dāng)然也可以配合^來排除字符氮采。
搜索包含數(shù)字的行
# grep -n '[0-9]' regular_express.txt
5:However ,this dress is about # 3183 dollars.
15:You are the best is menu you are the no.1.
行首與行尾字符 ^ #. ^ 表示行的開頭体捏,#表示行的結(jié)尾( 不是字符,是位置)那么‘^#' 就表示空行,因?yàn)?/p>
只有行首和行尾梗摇。
這里^與[]里面使用的^意義不同拓哟。它表示^后面的串是在行的開頭。
比如搜索the在開頭的行
# grep -n '^the' regular_express.txt
12:the symbol '*' is represented as star.
搜索以小寫字母開頭的行
# 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 star.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
#
搜索開頭不是英文字母的行
# grep -n '^[^a-zA-Z]' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
21:#I am VBird
#
#表示它前面的串是在行的結(jié)尾伶授,比如 '\.' 表示 . 在一行的結(jié)尾
搜索末尾是.的行
# grep -n '\.#' regular_express.txt //. 是正則表達(dá)式的特殊符號断序,所以要用
\轉(zhuǎn)義
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.
.....
注意在MS的系統(tǒng)下生成的文本文件,換行會加上一個 ^M 字符糜烹。所以最后的字符會是隱藏的^M ,在處理
Windows下面的文本時要特別注意违诗!
可以用cat dos_file | tr -d '\r' > unix_file 來刪除^M符號。 ^M==\r
那么'^#' 就表示只有行首行尾的空行拉景图!
搜索空行
# grep -n '^#' regular_express.txt
22:
23:
#
搜索非空行
# grep -vn '^#' 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.
..........
任意一個字符. 與重復(fù)字符 *
在bash中*代表通配符较雕,用來代表任意個 字符,但是在正則表達(dá)式中,他含義不同亮蒋,*表示有0個或多
個某個字符扣典。
例如 oo*, 表示第一個o一定存在,第二個o可以有一個或多個慎玖,也可以沒有贮尖,因此代表至少一個o.
點(diǎn). 代表一個任意字符,必須存在趁怔。 g??d 可以用 'g..d' 表示湿硝。 good ,gxxd ,gabd .....都符合。
# 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'.
#
搜索兩個o以上的字符串
# 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開頭和結(jié)尾铺浇,中間是至少一個o的字符串痢畜,即gog, goog....gooog...等
# grep -n 'goo*g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
搜索g開頭和結(jié)尾的字符串在的行
# 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.
限定連續(xù)重復(fù)字符的范圍 { }
. * 只能限制0個或多個, 如果要確切的限制字符重復(fù)數(shù)量鳍侣,就用{范圍} 丁稀。范圍是數(shù)字用,隔開 2,5 表示
2~5個, 2表示2個,2, 表示2到更多個
注意倚聚,由于{ }在SHELL中有特殊意義线衫,因此作為正則表達(dá)式用的時候要用\轉(zhuǎn)義一下。
搜索包含兩個o的字符串的行惑折。
# 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的字符串的行授账。
# grep -n 'go\{2,5\}g' regular_express.txt
18:google is the best tools for search keyword.
搜索包含g后面跟2個以上o,后面再跟g的行。唬复。
# grep -n 'go\{2,\}g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
注意矗积,相讓[]中的^ - 不表現(xiàn)特殊意義,可以放在[]里面內(nèi)容的后面敞咧。
'[^a-z\.!^ -]' 表示沒有小寫字母棘捣,沒有. 沒有!, 沒有空格,沒有- 的 串休建,注意[]里面有個小空格乍恐。
另外shell 里面的反向選擇為[!range], 正則里面是 [^range]
2擴(kuò)展正則表達(dá)式
擴(kuò)展正則表達(dá)式是對基礎(chǔ)正則表達(dá)式添加了幾個特殊構(gòu)成的。
它令某些操作更加方便测砂。
比如我們要去除 空白行和行首為 #的行茵烈, 會這樣用:
# 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.
............
然而使用支持?jǐn)U展正則表達(dá)式的 egrep 與擴(kuò)展特殊符號 | ,會方便許多砌些。
注意grep只支持基礎(chǔ)表達(dá)式呜投, 而egrep 支持?jǐn)U展的加匈, 其實(shí) egrep 是 grep -E 的別名而已。因此grep -
E 支持?jǐn)U展正則仑荐。
那么:
# 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.
....................
這里| 表示或的關(guān)系雕拼。 即滿足 ^# 或者 ^# 的字符串。
這里列出幾個擴(kuò)展特殊符號:
+粘招, 于 . * 作用類似啥寇,表示 一個或多個重復(fù)字符。
?洒扎, 于 . * 作用類似辑甜,表示0個或一個字符。
|袍冷,表示或關(guān)系磷醋,比如 'gd|good|dog' 表示有g(shù)d,good或dog的串
(),將部分內(nèi)容合成一個單元組难裆。 比如 要搜索 glad 或 good 可以這樣 'g(la|oo)d'
()的好處是可以對小組使用 + ? * 等子檀。
比如要搜索A和C開頭結(jié)尾镊掖,中間有至少一個(xyz) 的串乃戈,可以這樣 : 'A(xyz)+C'
詳細(xì)出處參考:http://www.jb51.net/article/31207.htm