? 本文主要講述shell正則表達式的主要概念和常用“三劍客”之一的grep命令
創(chuàng)新互聯(lián)公司長期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為臨縣企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、成都網(wǎng)站制作,臨縣網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
? 正則表達式:或稱正規(guī)表達式、常規(guī)表達式。是使用單個字符串來描述、匹配一系列符合某個句法規(guī)則的字符串,即通過一些特殊符號實現(xiàn)快速查找、刪除、替換某個特定字符串。由普通字符和元字符組成的文字模式。
? 普通字符:如大小寫字母、數(shù)字、標(biāo)點符號以及一些其他符號
? 元字符:具有特殊意義的專用字符,可以用來規(guī)定其前導(dǎo)字符(即位于元字符前面的字符)在目標(biāo)對象中的出現(xiàn)模式。
? 當(dāng)然這些概念未免太過抽象而且枯燥,下面的實例或許可以有助于您理解這些抽象的概念。
正則表達式包括:
系統(tǒng)管理員常用,且是必備技能之一。有助于快速定位重要信息,解決相關(guān)問題。
下面結(jié)合實例細講grep命令在正則表達式的作用與使用格式方法,從而引出基本正則表達式所包含的元字符的含義。
grep命令
[root@lokott opt]# cat test.txt //測試文本內(nèi)容
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
1)查找特定字符
[root@lokott opt]# grep -n 'the' test.txt //顯示行號檢索含有the的行
4:the tongue is boneless but it breaks bones.12!
5: google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
[root@lokott opt]# grep -ni 'the' test.txt //顯示行號,不區(qū)分大小寫檢索含有the的行
3:The home of Football on BBC Sport online.
4:the tongue is boneless but it breaks bones.12!
5: google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
[root@lokott opt]# grep -nv 'the' test.txt //顯示行號,檢索不帶the的行
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
7:PI=3.141592653589793238462643383249901429
8:a wood cross!
9:Actions speak louder than words
10:
11:
12:#woood #
13:#woooooood #
14:AxyzxyzxyzxyzC
15:I bet this place is really spooky late at night!
16:Misfortunes never come alone/single.
17:I shouldn't have lett so tast.
2)下面利用中括號[ ]來查找集合字符
[root@lokott opt]# grep -n 'sh[io]rt' test.txt //檢索包含shirt或者short的行
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
[root@lokott opt]# grep -n 'oo' test.txt //重復(fù)字符檢索
3:The home of Football on BBC Sport online.
5: google is the best tools for search keyword.
8:a wood cross!
12:#woood #
13:#woooooood #
15:I bet this place is really spooky late at night!
[root@lokott opt]# grep -n '[^w]oo' test.txt //檢索oo字符的前導(dǎo)字符為非w的行
3:The home of Football on BBC Sport online.
5: google is the best tools for search keyword.
12:#woood # //這里匹配的是后面的兩個o,其前導(dǎo)字符為第一個o,所以顯示的時候這三個o為紅色
13:#woooooood # //這里匹配的是第一個到第6個o
15:I bet this place is really spooky late at night!
[root@lokott opt]# grep -n '[^a-z]oo' test.txt //匹配字符串oo前面為非小寫字母的行,匹配的是Foo
3:The home of Football on BBC Sport online.
[root@lokott opt]# grep -n '[0-9]' test.txt //匹配數(shù)字字符
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429
3)查找行首^與行尾字符$
行首檢索實例:
[root@lokott opt]# grep -n '^the' test.txt //檢索以the開頭的行
4:the tongue is boneless but it breaks bones.12!
[root@lokott opt]# grep -n '^[a-z]' test.txt //檢索以小寫字母開頭的行
1:he was short and fat.
4:the tongue is boneless but it breaks bones.12!
8:a wood cross!
[root@lokott opt]# grep -n '^[a-zA-Z]' test.txt //檢索以字母開頭的行
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
4:the tongue is boneless but it breaks bones.12!
6:The year ahead will test our political establishment to the limit.
7:PI=3.141592653589793238462643383249901429
8:a wood cross!
9:Actions speak louder than words
14:AxyzxyzxyzxyzC
15:I bet this place is really spooky late at night!
16:Misfortunes never come alone/single.
17:I shouldn't have lett so tast.
[root@lokott opt]# grep -n '^[^a-zA-Z]' test.txt //檢索不是以字母開頭的行
5: google is the best tools for search keyword.
12:#woood #
13:#woooooood #
注意:!這里的^所處的位置所代表的含義是不一樣的,在中括號外面的表示取以括號內(nèi)的內(nèi)容開頭,反之表示以內(nèi)容取反。
簡單來說,16字概括:中括號外,以內(nèi)開頭,中括號內(nèi),以內(nèi)取反。
行尾$檢索實例:
[root@lokott opt]# grep -n '\.$' test.txt //檢索以點“.”結(jié)尾的行
1:he was short and fat.
3:The home of Football on BBC Sport online.
5: google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
16:Misfortunes never come alone/single.
17:I shouldn't have lett so tast.
[root@lokott opt]# grep -n '^$' test.txt //檢索出空行
10:
11:
注意:??!"."代表點的意思的時候,進行查找需要使用" \ " 轉(zhuǎn)義,因為點號“.”也是元字符
4)查找任意一個字符“.”與重復(fù)字符“*”
[root@lokott opt]# grep -n 'w..d' test.txt //檢索w和d之間可以是任意兩個字符的行
5: google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
[root@lokott opt]# grep -n 'wo*d' test.txt //檢索w和d之間o出現(xiàn)0次或者多次的行
8:a wood cross!
12:#woood #
13:#woooooood #
[root@lokott opt]# grep -n 'ooo*d' test.txt //檢索第三個o出現(xiàn)0次或者多次的行
8:a wood cross!
12:#woood #
13:#woooooood #
[root@lokott opt]# grep -n 'w.*d' test.txt //檢索w與d之間可有可無的字符的行
1:he was short and fat.
5: google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
12:#woood #
13:#woooooood #
[root@lokott opt]# grep -n '[0-9][0-9]*' test.txt //檢索任意數(shù)字所在的行
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429
5)查找連續(xù)字符范圍"{}"
? {}主要作用是為了限制一個范圍內(nèi)重復(fù)的字符串,例如查找3-5個o的連續(xù)字符即可需要使用{},但是由于在shell中其有特定意義,所以需要利用轉(zhuǎn)義字符“\”,將“{}”字符轉(zhuǎn)換成普通字符。
[root@lokott opt]# grep -n 'o\{2\}' test.txt //檢索連續(xù)兩個字符“o”的行
3:The home of Football on BBC Sport online.
5: google is the best tools for search keyword.
8:a wood cross!
12:#woood #
13:#woooooood #
15:I bet this place is really spooky late at night!
[root@lokott opt]# grep -n 'wo\{2,5\}d' test.txt //檢索以w開頭d結(jié)尾o出現(xiàn)2-5次的行
8:a wood cross!
12:#woood #
[root@lokott opt]# grep -n 'wo\{2,\}d' test.txt //檢索w開頭d結(jié)尾o出現(xiàn)2次以上的行
8:a wood cross!
12:#woood #
13:#woooooood #
基礎(chǔ)正則表達式常見元字符總結(jié)
^——上述16字口訣
$——匹配輸入字符串結(jié)尾位置
.——匹配除了“\r\n”的任何單個字符
\——將下一個字符標(biāo)記為特殊字符,原意字符、向后引用、八進制轉(zhuǎn)義符。
*——匹配前面的前導(dǎo)字符出現(xiàn)0次或者多次
[]——字符集合。匹配所包含的任意一個字符。
[^]——賦值字符集合。匹配未包含的一個任意字符。
[n1-n2]——字符范圍。匹配指定范圍內(nèi)的任意一個字符。
{n}——n為非負(fù)整數(shù),匹配確定的n次。
{n,}——n為非負(fù)整數(shù),至少匹配n次
{n1,n2}——n1和n2都是非負(fù)整數(shù),n1<n2,匹配此時介于n1-n2之間。
? 本文主要是借grep命令引出基礎(chǔ)正則表達式的基本概念與用法,介紹了如何使用基礎(chǔ)正則表達式以及對元字符進行解釋與常用元字符的總結(jié)。
新聞名稱:shell腳本編程之正則表達式(一)(基礎(chǔ)正則表達式、grep)
URL地址:http://jinyejixie.com/article6/ggecog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、軟件開發(fā)、面包屑導(dǎo)航、網(wǎng)站制作、云服務(wù)器、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)