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

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

Shell腳本之正則表達式

一.正則表達式三劍客之一:grep

1.學習正則表達式前咱們拿一個無用的配置文件做爲測試練習正則表達式

[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.查找特定字符redis

「-vn」 反向選擇。查找不包含「the」字符的行,則須要經過 grep 命令的「-vn」選項實現。
-n「 表示顯示行號
「-i」 表示不區分大小寫
命令執行後,符合匹配標準的字符,字體顏色會變爲紅色shell

[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.括號"[ ]"來查找集合字符
想要查找「shirt」與「short」這兩個字符串時,能夠發現這兩個字符串均包含「sh」 與「rt」。此時執行如下命令便可同時查找到「shirt」與「short」這兩個字符串。「[]」中不管有幾個字符,都僅表明一個字符,也就是說「[io]」表示匹配「i」或者「o」。vim

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

若要查找包含重複單個字符「oo」時,只須要執行如下命令便可。ide

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

若查找「oo」前面不是「w」的字符串,只須要經過集合字符的反向選擇「[^]」來實現該目的,如執行「grep –n‘[^w]oo’test.txt」命令表示在 test.txt 文本中查找「oo」 前面不是「w」的字符串學習

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

在上述命令的執行結果中發現「woood」與「wooooood」也符合匹配規則,兩者均包含「w」。其實經過執行結果就能夠看出,符合匹配標準的字符加粗顯示,而上述結果中能夠得知,「#woood #」中加粗顯示的是「ooo」,而「oo」前面的「o」是符合匹配規則的。同理 「#woooooood #」也符合匹配規則。
若不但願「oo」前面存在小寫字母,可使用「grep –n‘[^a-z]oo’test.txt」命令實現,其中「a-z」表示小寫字母,大寫字母則經過「A-Z」表示。測試

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

查找包含數字的行能夠經過「grep –n‘[0-9]’test.txt」命令來實現字體

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

查找行首「^」與行尾字符「$」orm

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

查詢以小寫字母開頭的行能夠經過「1」規則來過濾,

[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

查詢大寫字母開頭

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

若查詢不以字母開頭的行則使用「[a-zA-Z]」規則。

[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

「^」符號在元字符集合「[]」符號內外的做用是不同的,在「[]」符號內表示反向選擇,在「[]」符號外則表明定位行首。反之,若想查找以某一特定字符結尾的行則可使用「$」定位符。例如,執行如下命令便可實現查詢以小數點(.)結尾的行。由於小數點(.) 在正則表達式中也是一個元字符(後面會講到),因此在這裏須要用轉義字符「\」將具備特 殊意義的字符轉化成普通字符。

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

當查詢空白行時,執行「grep –n ‘^$’ chen.txt

查找任意一個字符「.」與重複字符「*」
在正則表達式中小數點(.)也是一個元字符,表明任意一個字符。例如, 執行如下命令就能夠查找「w??d」的字符串,即共有四個字符,以 w 開頭 d 結尾。

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

在上述結果中,「wood」字符串「w…d」匹配規則。若想要查詢 oo、ooo、ooooo 等資料,則須要使用星號()元字符。但須要注意的是,「」表明的是重複零個或多個前面的單字符。「o」表示擁有零個(即爲空字符)或大於等於一個「o」的字符,由於容許空字符,因此執行「grep –n‘o’test.txt」命令會將文本中全部的內容都輸出打印。若是是「oo」, 則第一個 o 必須存在,第二個 o 則是零個或多個 o,因此凡是包含 o、oo、ooo、ooo,等的資料都符合標準。同理,若查詢包含至少兩個 o 以上的字符串,則執行「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

查詢以 w 開頭 d 結尾,中間包含至少一個 o 的字符串,執行如下命令便可實現。

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

查詢以 w 開頭 d 結尾,中間的字符無關緊要的字符串。

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

查詢任意數字所在行。

[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

查找連續字符範圍「{}」
使用「.」與「*」來設定零個到無限多個重複的字符,若是想要限制一個範圍內的重複的字符串該如何實現呢?例如,查找三到五個 o 的連續字符,這個時候就須要使用基礎正則表達式中的限定範圍的字符「{}」。由於「{}」在 Shell 中具備特殊 意義,因此在使用「{}」字符時,須要利用轉義字符「\」,將「{}」字符轉換成普通字符。

查詢兩個 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

查詢以 w 開頭以 d 結尾,中間包含 2~5 個 o 的字符串。

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

查詢以 w 開頭以 d 結尾,中間包含 2 以上 o 的字符串。

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

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

二.擴展正則表達式

爲了簡化整個指令,須要使用範圍更廣的擴展正則表達式。例如,使用基礎正則表達式查詢除文件中空白行與行首爲「#」 以外的行(一般用於查看生效的配置文件),執行「grep –v‘^KaTeX parse error: Expected group after '^' at position 22: …txt | grep –v ‘^̲#’」便可實現。這裏須要使用管…|^#’test.txt」,其中,單引號內的管道符號表示或者(or)。
此外,grep 命令僅支持基礎正則表達式,若是使用擴展正則表達式,須要使用 egrep 或 awk 命令。awk 命令在後面的小節進行講解,這裏咱們直接使用 egrep 命令。egrep 命令與 grep 命令的用法基本類似。egrep 命令是一個搜索文件得到模式,使用該命令能夠搜索文件中的任意字符串和符號,也能夠搜索一個或多個文件的字符串,一個提示符能夠是單個字符、一個字符串、一個字或一個句子。
常見的擴展正則表達式的元字符主要包括如下幾個:

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

"+「示例:執行「egrep -n ‘wo+d’ test.txt」命令,便可查詢"wood」 「woood」 "woooooood"等字符串

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

"?"示例:執行「egrep -n ‘bes?t’ test.txt」命令,便可查詢「bet」「best」這兩個字符串

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

"|"示例:執行「egrep -n ‘of|is|on’ test.txt」命令便可查詢"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」由於這兩個單詞的「t」與「st」是重複的,因此將「a」與「e」列於「()」符號當中,並以「|」分隔,便可查詢"tast"或者"test"字符串

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

"()+「示例:「egrep -n ‘A(xyz)+C’ test.txt」。該命令是查詢開頭的"A"結尾是"C」,中間有一個以上的 "xyz"字符串的意思

[root@localhost ~]# egrep -n 'A(xyz)+C' chen.txt
14:AxyzxyzxyzC
相關文章
相關標籤/搜索