linux中的grep命令的用法詳解(2)
查找非小寫字母加oo的內(nèi)容
[root@ www.linuxidc.com]# grep -n '[^a-z]oo' regular_express.txt
3:Football game is not use feet only.
獲取有數(shù)字的一行
[root@ www.linuxidc.com]# grep -n '[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.
[root@ www.linuxidc.com]# grep -n '[^[:lower:]]oo' regular_express.txt
3:Football game is not use feet only.
查詢以the開頭的字符
[root@ www.linuxidc.com]# grep -n '^the' regular_express.txt
12:the symbol '*' is represented as start.
查詢開頭是小寫字母的內(nèi)容
[root@ www.linuxidc.com]# 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.
查詢第一個(gè)字符不是大寫的
[root@ www.linuxidc.com]# grep -n '^[[:lower:]]' 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@ www.linuxidc.com]# grep -n '^[^a-zA-Z]' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
21:# I am VBird
查詢結(jié)尾是小數(shù)點(diǎn)的行的內(nèi)容.
[root@ www.linuxidc.com]# 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@ www.linuxidc.com]# grep -n '^$' regular_express.txt
22:
通配符.和*的使用,.(小數(shù)點(diǎn))代表一定有一個(gè)任意字符,*代表重復(fù)前一個(gè)到無窮多次的意思
[root@ www.linuxidc.com]# 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".
查詢出現(xiàn)任意數(shù)字的行列
[root@ www.linuxidc.com]# 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.
查詢出現(xiàn)兩個(gè)o的字符串
[root@ www.linuxidc.com]# 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!
查詢出現(xiàn)2-5個(gè)o的字符串,后面在接一個(gè)g的字符串
[root@ www.linuxidc.com]# grep -n 'o\{2,5\}g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
查詢出現(xiàn)2個(gè)以上o的字符串,后面在接一個(gè)g的字符串
[root@ www.linuxidc.com]# grep -n 'o\{2,\}g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
三、擴(kuò)展資料:linux中g(shù)rep命令之正則表達(dá)式
匹配字符:這部分和基本正則表達(dá)式一樣
匹配次數(shù):
* :和基本正則表達(dá)式一樣
? :基本正則表達(dá)式是\?,二這里沒有\(zhòng)。
{m,n} :相比基本正則表達(dá)式也是沒有了\。
+ :匹配其前面的字符至少一次,相當(dāng)于{1,}。
位置錨定:和基本正則表達(dá)式一樣。
分組及引用:
(string) :相比基本正則表達(dá)式也是沒有了\。
class="main">
linux中的grep命令的用法詳解(2)
或者:
a|b :匹配a或b,注意a是指 | 的左邊的整體,b也同理。比如 C|cat 表示的是 C或cat,而不是Cat或cat,如果要表示Cat或cat,則應(yīng)該寫為 (C|c)at 。記住(string)除了用于引用還用于分組。
常用正則表達(dá)式部分就說到這里,以后用到在另行補(bǔ)充,如果你有興趣也可以去網(wǎng)上查找其他文章來進(jìn)一步了解。