溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

shell腳本編程之正則表達(dá)式(一)(基礎(chǔ)正則表達(dá)式、grep)

發(fā)布時(shí)間:2020-07-13 20:45:18 來源:網(wǎng)絡(luò) 閱讀:269 作者:wx5d8a17c45cb5b 欄目:系統(tǒng)運(yùn)維

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

一、前言

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

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

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

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

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

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

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

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

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

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

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

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

grep命令

  1. -n——顯示行號(hào)
  2. -i——忽略大小寫
  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ū)分大小寫檢索含有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)[ ]來查找集合字符

[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前面為非小寫字母的行,匹配的是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開頭的行
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 # 

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

簡(jiǎn)單來說,16字概括:中括號(hào)外,以內(nè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:

注意:?。?quot;."代表點(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之間可有可無的字符的行
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開頭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 # 

五、元字符總結(jié)

基礎(chǔ)正則表達(dá)式常見元字符總結(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é)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI