shell腳本正則表達(dá)式三劍客之一(grep,egrep)

shell腳本正則表達(dá)式三劍客之一(grep,egrep)

10年積累的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶(hù)對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶(hù)得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有新興免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

Shell腳本之正則表達(dá)式

一.正則表達(dá)式三劍客之一:grep

1.學(xué)習(xí)正則表達(dá)式前我們拿一個(gè)無(wú)用的配置文件作為測(cè)試練習(xí)

[root@localhost ~]# vim chen.txt

#version=DEVEL
 System authorization information
auth --enableshadow --passalgo=sha512# Use CDROM installation media
cdrom
thethethe
THE
THEASDHAS
 Use graphical install
graphical
 Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
wood
wd
wod
woooooooood
124153
3234
342222222
faasd11
2
ZASASDNA
short
shirt

2.查找特定字符

“-vn” 反向選擇。查找不包含“the”字符的行,則需要通過(guò) grep 命令的“-vn”選項(xiàng)實(shí)現(xiàn)。
-n“ 表示顯示行號(hào)
“-i” 表示不區(qū)分大小寫(xiě)
命令執(zhí)行后,符合匹配標(biāo)準(zhǔn)的字符,字體顏色會(huì)變?yōu)榧t色

[root@localhost ~]# grep -n 'the' chen.txt
6:thethethe
11:# Run the Setup Agent on first boot
[root@localhost ~]# grep -in 'the' chen.txt
6:thethethe
7:THE
8:THEASDHAS
11:# Run the Setup Agent on first boot
[root@localhost ~]# grep -vn 'the' chen.txt
1:#version=DEVEL
2:# System authorization information
3:auth --enableshadow --passalgo=sha512
4:# Use CDROM installation media
5:cdrom
7:THE
8:THEASDHAS
9:# Use graphical install
10:graphical
12:firstboot --enable
13:ignoredisk --only-use=sda
14:wood
15:wd
16:wod
17:woooooooood
18:124153
19:3234
20:342222222
21:faasd11
22:2
23:ZASASDNA
24:
short
shirt

3.括號(hào)"[ ]"來(lái)查找集合字符
想要查找“shirt”與“short”這兩個(gè)字符串時(shí),可以發(fā)現(xiàn)這兩個(gè)字符串均包含“sh” 與“rt”。此時(shí)執(zhí)行以下命令即可同時(shí)查找到“shirt”與“short”這兩個(gè)字符串?!癧]”中無(wú)論有幾個(gè)字符,都僅代表一個(gè)字符,也就是說(shuō)“[io]”表示匹配“i”或者“o”。

[root@localhost ~]# grep -n 'sh[io]rt' chen.txt  //過(guò)濾short或shirt中都有io集合字符
24:short
25:shirt

若要查找包含重復(fù)單個(gè)字符“oo”時(shí),只需要執(zhí)行以下命令即可。

[root@localhost ~]# grep -n 'oo' chen.txt 
11:# Run the Setup Agent on first boot
12:firstboot --enable
14:wood
17:woooooooood

若查找“oo”前面不是“w”的字符串,只需要通過(guò)集合字符的反向選擇“[^]”來(lái)實(shí)現(xiàn)該目的,如執(zhí)行“grep –n‘[^w]oo’test.txt”命令表示在 test.txt 文本中查找“oo” 前面不是“w”的字符串

[root@localhost ~]# grep -n '[^w]oo' chen.txt //過(guò)濾w開(kāi)頭oo的字符串
11:# Run the Setup Agent on first boot
12:firstboot --enable
17:woooooooood

在上述命令的執(zhí)行結(jié)果中發(fā)現(xiàn)“woood”與“wooooood”也符合匹配規(guī)則,二者均包含“w”。其實(shí)通過(guò)執(zhí)行結(jié)果就可以看出,符合匹配標(biāo)準(zhǔn)的字符加粗顯示,而上述結(jié)果中可以得知,“#woood #”中加粗顯示的是“ooo”,而“oo”前面的“o”是符合匹配規(guī)則的。同理 “#woooooood #”也符合匹配規(guī)則。
若不希望“oo”前面存在小寫(xiě)字母,可以使用“grep –n‘[^a-z]oo’test.txt”命令實(shí)現(xiàn),其中“a-z”表示小寫(xiě)字母,大寫(xiě)字母則通過(guò)“A-Z”表示。

[root@localhost ~]# grep -n '[^a-z]oo' chen.txt 
19:Foofddd

查找包含數(shù)字的行可以通過(guò)“grep –n‘[0-9]’test.txt”命令來(lái)實(shí)現(xiàn)

[root@localhost ~]# grep -n '[0-9]' chen.txt
3:auth --enableshadow --passalgo=sha512
20:124153
21:3234
22:342222222
23:faasd11
24:2

查找行首“^”與行尾字符“$”

[root@localhost ~]# grep -n '^the' chen.txt
6:thethethe

查詢(xún)以小寫(xiě)字母開(kāi)頭的行可以通過(guò)“1”規(guī)則來(lái)過(guò)濾,

[root@localhost ~]# grep -n '^[a-z]' chen.txt
3:auth --enableshadow --passalgo=sha512
5:cdrom
6:thethethe
10:graphical
12:firstboot --enable
13:ignoredisk --only-use=sda
14:wood
15:wd
16:wod
17:woooooooood
18:dfsjdjoooooof
23:faasd11
26:short
27:shirt

查詢(xún)大寫(xiě)字母開(kāi)頭

[root@localhost ~]# grep -n '^[A-Z]' chen.txt
7:THE
8:THEASDHAS
19:Foofddd
25:ZASASDNA

若查詢(xún)不以字母開(kāi)頭的行則使用“[a-zA-Z]”規(guī)則。

[root@localhost ~]# grep -n '^[^a-zA-Z]' chen.txt
1:#version=DEVEL
2:# System authorization information
4:# Use CDROM installation media
9:# Use graphical install
11:# Run the Setup Agent on first boot
20:124153
21:3234
22:342222222
24:2

“^”符號(hào)在元字符集合“[]”符號(hào)內(nèi)外的作用是不一樣的,在“[]”符號(hào)內(nèi)表示反向選擇,在“[]”符號(hào)外則代表定位行首。反之,若想查找以某一特定字符結(jié)尾的行則可以使用“$”定位符。例如,執(zhí)行以下命令即可實(shí)現(xiàn)查詢(xún)以小數(shù)點(diǎn)(.)結(jié)尾的行。因?yàn)樾?shù)點(diǎn)(.) 在正則表達(dá)式中也是一個(gè)元字符(后面會(huì)講到),所以在這里需要用轉(zhuǎn)義字符“\”將具有特 殊意義的字符轉(zhuǎn)化成普通字符。

[root@localhost ~]# grep -n '\.$' chen.txt
5:cdrom.
6:thethethe.
9:# Use graphical install.
10:graphical.
11:# Run the Setup Agent on first boot.

當(dāng)查詢(xún)空白行時(shí),執(zhí)行“grep –n ‘^$’ chen.txt

查找任意一個(gè)字符“.”與重復(fù)字符“*”
在正則表達(dá)式中小數(shù)點(diǎn)(.)也是一個(gè)元字符,代表任意一個(gè)字符。例如, 執(zhí)行以下命令就可以查找“w??d”的字符串,即共有四個(gè)字符,以 w 開(kāi)頭 d 結(jié)尾。

[root@localhost ~]# grep -n 'w..d' chen.txt
14:wood

在上述結(jié)果中,“wood”字符串“w…d”匹配規(guī)則。若想要查詢(xún) oo、ooo、ooooo 等資料,則需要使用星號(hào)()元字符。但需要注意的是,“”代表的是重復(fù)零個(gè)或多個(gè)前面的單字符?!皁”表示擁有零個(gè)(即為空字符)或大于等于一個(gè)“o”的字符,因?yàn)樵试S空字符,所以執(zhí)行“grep –n‘o’test.txt”命令會(huì)將文本中所有的內(nèi)容都輸出打印。如果是“oo”, 則第一個(gè) o 必須存在,第二個(gè) o 則是零個(gè)或多個(gè) o,所以凡是包含 o、oo、ooo、ooo,等的資料都符合標(biāo)準(zhǔn)。同理,若查詢(xún)包含至少兩個(gè) o 以上的字符串,則執(zhí)行“grep –n‘ooo’ test.txt”命令即可。

[root@localhost ~]# grep -n 'ooo*' chen.txt
11:# Run the Setup Agent on first boot.
12:firstboot --enable
14:wood
17:woooooooood
18:dfsjdjoooooof
19:Foofddd

查詢(xún)以 w 開(kāi)頭 d 結(jié)尾,中間包含至少一個(gè) o 的字符串,執(zhí)行以下命令即可實(shí)現(xiàn)。

[root@localhost ~]# grep -n 'woo*d' chen.txt
14:wood
16:wod
17:woooooooood

查詢(xún)以 w 開(kāi)頭 d 結(jié)尾,中間的字符可有可無(wú)的字符串。

[root@localhost ~]# grep -n 'w.*d' chen.txt
14:wood
15:wd
16:wod
17:woooooooood

查詢(xún)?nèi)我鈹?shù)字所在行。

[root@localhost ~]# grep -n '[0-9][0-9]*' chen.txt
3:auth --enableshadow --passalgo=sha512
20:124153
21:3234
22:342222222
23:faasd11
24:2

查找連續(xù)字符范圍“{}”
使用“.”與“*”來(lái)設(shè)定零個(gè)到無(wú)限多個(gè)重復(fù)的字符,如果想要限制一個(gè)范圍內(nèi)的重復(fù)的字符串該如何實(shí)現(xiàn)呢?例如,查找三到五個(gè) o 的連續(xù)字符,這個(gè)時(shí)候就需要使用基礎(chǔ)正則表達(dá)式中的限定范圍的字符“{}”。因?yàn)椤皗}”在 Shell 中具有特殊 意義,所以在使用“{}”字符時(shí),需要利用轉(zhuǎn)義字符“\”,將“{}”字符轉(zhuǎn)換成普通字符。

查詢(xún)兩個(gè) o 以上的字符

[root@localhost ~]# grep -n 'o\{2\}' chen.txt
11:# Run the Setup Agent on first boot.
12:firstboot --enable
14:wood
17:woooooooood
18:dfsjdjoooooof
19:Foofddd

查詢(xún)以 w 開(kāi)頭以 d 結(jié)尾,中間包含 2~5 個(gè) o 的字符串。

[root@localhost ~]# grep -n 'wo\{2,5\}d' chen.txt
14:wood

查詢(xún)以 w 開(kāi)頭以 d 結(jié)尾,中間包含 2 以上 o 的字符串。

[root@localhost ~]# grep -n 'wo\{2,\}d' chen.txt
14:wood
17:woooooooood

shell腳本正則表達(dá)式三劍客之一(grep,egrep)

二.擴(kuò)展正則表達(dá)式

為了簡(jiǎn)化整個(gè)指令,需要使用范圍更廣的擴(kuò)展正則表達(dá)式。例如,使用基礎(chǔ)正則表達(dá)式查詢(xún)除文件中空白行與行首為“#” 之外的行(通常用于查看生效的配置文件),執(zhí)行“grep –v‘^KaTeX parse error: Expected group after '^' at position 22: …txt | grep –v ‘^?#’”即可實(shí)現(xiàn)。這里需要使用管…|^#’test.txt”,其中,單引號(hào)內(nèi)的管道符號(hào)表示或者(or)。
此外,grep 命令僅支持基礎(chǔ)正則表達(dá)式,如果使用擴(kuò)展正則表達(dá)式,需要使用 egrep 或 awk 命令。awk 命令在后面的小節(jié)進(jìn)行講解,這里我們直接使用 egrep 命令。egrep 命令與 grep 命令的用法基本相似。egrep 命令是一個(gè)搜索文件獲得模式,使用該命令可以搜索文件中的任意字符串和符號(hào),也可以搜索一個(gè)或多個(gè)文件的字符串,一個(gè)提示符可以是單個(gè)字符、一個(gè)字符串、一個(gè)字或一個(gè)句子。
常見(jiàn)的擴(kuò)展正則表達(dá)式的元字符主要包括以下幾個(gè):

shell腳本正則表達(dá)式三劍客之一(grep,egrep)

"+“示例:執(zhí)行“egrep -n ‘wo+d’ test.txt”命令,即可查詢(xún)"wood” “woood” "woooooood"等字符串

[root@localhost ~]# egrep -n 'wo+d' chen.txt
14:wood
16:wod
17:woooooooood

"?"示例:執(zhí)行“egrep -n ‘bes?t’ test.txt”命令,即可查詢(xún)“bet”“best”這兩個(gè)字符串

[root@localhost ~]# egrep -n 'bes?t' chen.txt
11:best
12:bet

"|"示例:執(zhí)行“egrep -n ‘of|is|on’ test.txt”命令即可查詢(xún)"of"或者"if"或者"on"字符串

[root@localhost ~]# egrep -n 'of|is|on' chen.txt
1:#version=DEVEL
2:# System authorization information
4:# Use CDROM installation media
13:# Run the Setup Agent on first boot.
15:ignoredisk --only-use=sda
20:dfsjdjoooooof
21:Foofddd

"()"示例:“egrep -n ‘t(a|e)st’ test.txt”。“tast”與“test”因?yàn)檫@兩個(gè)單詞的“t”與“st”是重復(fù)的,所以將“a”與“e”列于“()”符號(hào)當(dāng)中,并以“|”分隔,即可查詢(xún)"tast"或者"test"字符串

[root@localhost ~]# egrep -n 't(a|e)st' chen.txt
12:test
13:tast

"()+“示例:“egrep -n ‘A(xyz)+C’ test.txt”。該命令是查詢(xún)開(kāi)頭的"A"結(jié)尾是"C”,中間有一個(gè)以上的 "xyz"字符串的意思

[root@localhost ~]# egrep -n 'A(xyz)+C' chen.txt
14:AxyzxyzxyzC

網(wǎng)頁(yè)標(biāo)題:shell腳本正則表達(dá)式三劍客之一(grep,egrep)
瀏覽路徑:http://bm7419.com/article4/jcsjoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、標(biāo)簽優(yōu)化用戶(hù)體驗(yàn)、商城網(wǎng)站、建站公司、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)

廣告

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

網(wǎng)站優(yōu)化排名