欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

shell腳本編程之正則表達(dá)式(一)(基礎(chǔ)正則表達(dá)式、grep)-創(chuàng)新互聯(lián)

shell腳本編程之正則表達(dá)式(一)

一、前言

? 本文主要講述shell正則表達(dá)式的主要概念和常用“三劍客”之一的grep命令

安平網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站開(kāi)發(fā)等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)從2013年開(kāi)始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。

二、正則表達(dá)式的定義

? 正則表達(dá)式:或稱正規(guī)表達(dá)式、常規(guī)表達(dá)式。是使用單個(gè)字符串來(lái)描述、匹配一系列符合某個(gè)句法規(guī)則的字符串,即通過(guò)一些特殊符號(hào)實(shí)現(xiàn)快速查找、刪除、替換某個(gè)特定字符串。由普通字符和元字符組成的文字模式。

? 普通字符:如大小寫(xiě)字母、數(shù)字、標(biāo)點(diǎn)符號(hào)以及一些其他符號(hào)

? 元字符:具有特殊意義的專用字符,可以用來(lái)規(guī)定其前導(dǎo)字符(即位于元字符前面的字符)在目標(biāo)對(duì)象中的出現(xiàn)模式。

? 當(dāng)然這些概念未免太過(guò)抽象而且枯燥,下面的實(shí)例或許可以有助于您理解這些抽象的概念。

正則表達(dá)式包括:

  • 基礎(chǔ)正則表達(dá)式——grep與sed支持
  • 擴(kuò)展正則表達(dá)式——egrep與awk支持

三、正則表達(dá)式的作用

系統(tǒng)管理員常用,且是必備技能之一。有助于快速定位重要信息,解決相關(guān)問(wèn)題。

四、正則表達(dá)式示例

下面結(jié)合實(shí)例細(xì)講grep命令在正則表達(dá)式的作用與使用格式方法,從而引出基本正則表達(dá)式所包含的元字符的含義。

grep命令

  1. -n——顯示行號(hào)
  2. -i——忽略大小寫(xiě)
  3. -v——反向查找
[root@lokott opt]# cat test.txt              //測(cè)試文本內(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      //顯示行號(hào)檢索含有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              //顯示行號(hào),不區(qū)分大小寫(xiě)檢索含有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     //顯示行號(hào),檢索不帶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)下面利用中括號(hào)[ ]來(lái)查找集合字符

[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 #            //這里匹配的是后面的兩個(gè)o,其前導(dǎo)字符為第一個(gè)o,所以顯示的時(shí)候這三個(gè)o為紅色
13:#woooooood #         //這里匹配的是第一個(gè)到第6個(gè)o
15:I bet this place is really spooky late at night! 
[root@lokott opt]# grep -n '[^a-z]oo' test.txt   //匹配字符串oo前面為非小寫(xiě)字母的行,匹配的是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)查找行首^與行尾字符$

行首檢索實(shí)例:

[root@lokott opt]# grep -n '^the' test.txt                   //檢索以the開(kāi)頭的行
4:the tongue is boneless but it breaks bones.12!
[root@lokott opt]# grep -n '^[a-z]' test.txt                //檢索以小寫(xiě)字母開(kāi)頭的行 
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               //檢索以字母開(kāi)頭的行
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      //檢索不是以字母開(kāi)頭的行
5: google is the best tools for search keyword.
12:#woood #
13:#woooooood #

注意:!這里的^所處的位置所代表的含義是不一樣的,在中括號(hào)外面的表示取以括號(hào)內(nèi)的內(nèi)容開(kāi)頭,反之表示以內(nèi)容取反。

簡(jiǎn)單來(lái)說(shuō),16字概括:中括號(hào)外,以內(nèi)開(kāi)頭,中括號(hào)內(nèi),以內(nèi)取反。

行尾$檢索實(shí)例:

[root@lokott opt]# grep -n '\.$' test.txt           //檢索以點(diǎn)“.”結(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:

注意:??!"."代表點(diǎn)的意思的時(shí)候,進(jìn)行查找需要使用" \ " 轉(zhuǎn)義,因?yàn)辄c(diǎn)號(hào)“.”也是元字符

4)查找任意一個(gè)字符“.”與重復(fù)字符“*”

[root@lokott opt]# grep -n 'w..d' test.txt                //檢索w和d之間可以是任意兩個(gè)字符的行
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             //檢索第三個(gè)o出現(xiàn)0次或者多次的行
8:a wood cross!
12:#woood #
13:#woooooood #
[root@lokott opt]# grep -n 'w.*d' test.txt               //檢索w與d之間可有可無(wú)的字符的行
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ù)字符范圍"{}"

? {}主要作用是為了限制一個(gè)范圍內(nèi)重復(fù)的字符串,例如查找3-5個(gè)o的連續(xù)字符即可需要使用{},但是由于在shell中其有特定意義,所以需要利用轉(zhuǎn)義字符“\”,將“{}”字符轉(zhuǎn)換成普通字符。

[root@lokott opt]# grep -n 'o\{2\}' test.txt     //檢索連續(xù)兩個(gè)字符“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開(kāi)頭d結(jié)尾o出現(xiàn)2-5次的行
8:a wood cross!
12:#woood #
[root@lokott opt]# grep -n 'wo\{2,\}d' test.txt   //檢索w開(kāi)頭d結(jié)尾o出現(xiàn)2次以上的行
8:a wood cross!
12:#woood #
13:#woooooood # 

五、元字符總結(jié)

基礎(chǔ)正則表達(dá)式常見(jiàn)元字符總結(jié)

^——上述16字口訣

$——匹配輸入字符串結(jié)尾位置

.——匹配除了“\r\n”的任何單個(gè)字符

\——將下一個(gè)字符標(biāo)記為特殊字符,原意字符、向后引用、八進(jìn)制轉(zhuǎn)義符。

*——匹配前面的前導(dǎo)字符出現(xiàn)0次或者多次

[]——字符集合。匹配所包含的任意一個(gè)字符。

[^]——賦值字符集合。匹配未包含的一個(gè)任意字符。

[n1-n2]——字符范圍。匹配指定范圍內(nèi)的任意一個(gè)字符。

{n}——n為非負(fù)整數(shù),匹配確定的n次。

{n,}——n為非負(fù)整數(shù),至少匹配n次

{n1,n2}——n1和n2都是非負(fù)整數(shù),n1<n2,匹配此時(shí)介于n1-n2之間。

六、小結(jié)

? 本文主要是借grep命令引出基礎(chǔ)正則表達(dá)式的基本概念與用法,介紹了如何使用基礎(chǔ)正則表達(dá)式以及對(duì)元字符進(jìn)行解釋與常用元字符的總結(jié)。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

當(dāng)前題目:shell腳本編程之正則表達(dá)式(一)(基礎(chǔ)正則表達(dá)式、grep)-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://chinadenli.net/article0/deesio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、微信小程序外貿(mào)網(wǎng)站建設(shè)、App開(kāi)發(fā)、軟件開(kāi)發(fā)、微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)

網(wǎng)站設(shè)計(jì)公司知識(shí)