Shell腳本正則運算式(一)
- 正則運算式概述
- 基礎正則運算式——grep,sed命令支持
- 基礎正則運算式常見元字符
- 擴展正則運算式----egrep、awk命令支持
正則運算式概述
1.正則運算式定義
1).正則運算式,又稱正規運算式、常規運算式
在代碼中常簡寫為regex 、regexp或PE
2).是使用單個字串來描述,匹配一系列符合某個句法規則的字串
例:郵件服務處于過濾垃圾郵箱,最常用正則運算式
2.正則運算式組成
1).普通字符
大小寫字母,數字,標點符號及一些其他符號
2).元字符
在正則運算式中具有特殊意義的專用字符
基礎正則運算式——grep,sed命令支持
1.基礎正則運算式示例
1).查找特定字符
| 命令 | 作用 |
|---|---|
| -n | 顯示行號 |
| -i | 不區分大小寫 |
| -v | 反向查找 |
創建測驗檔案
[root@localhost ~]# vim test.txt
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 li
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.
查找the并顯示行號
[root@localhost ~]# grep -n 'the' test.txt

查找the并不區分大小寫
[root@localhost ~]# grep -ni 'the' test.txt

反向查找不包含the的行
[root@localhost ~]# grep -nv 'the' test.txt

2).利用中括號"[ ]"來查找集合字符
[ ]---里面無論有多少字符,都代表一個字符,為'或'的關系
[^]---括號里面的'^'是取反的意思
查找包含shirt和short的行
[root@localhost ~]# grep -n 'sh[io]rt' test.txt

查找重復單個字符’oo’的行
[root@localhost ~]# grep -n 'oo' test.txt

查找’oo’前面不是’w’的行
[root@localhost ~]# grep -n '[^w]oo' test.txt

查找’oo’前面不是小寫字母的行
[root@localhost ~]# grep -n '[^a-z]oo' test.txt

查找’oo’前面不是大寫字母的行
[root@localhost ~]# grep -n '[^A-Z]oo' test.txt

查找包含數字的行
[root@localhost ~]# grep -n '[0-9]' test.txt

3).查找行首’^‘與行尾字符’$'
小數點’.‘在正則運算式中為元字符,需要使用轉義字符’'將其轉化為普通字符
查找以小數點’.'結尾的行
[root@localhost ~]# grep -n '\.$' test.txt

查找空行
[root@localhost ~]# grep -n '^$' test.txt

4).查找任意一個字符用’.’,重復字符用’*'
查找以’w’開頭,'d’結尾共4個字符的行
[root@localhost ~]# grep -n 'w..d' test.txt

*—表示重復零個或多個前面的單字符
例:'oo*'---第一個o必須存在,第二個o可以是0個或多個,所以o,oo,ooo,等都符合規則
查詢至少包含兩個o以上的字串
[root@localhost ~]# grep -n 'ooo*' test.txt

查找以’w’開頭,中間至少包含一個’o’的,'d’結尾的行
[root@localhost ~]# grep -n 'woo*d' test.txt

查找以’w’開頭,'d’結尾,中間字符可有可無的行
[root@localhost ~]# grep -n 'w.*d' test.txt

查詢任意數字的行
[root@localhost ~]# grep -n '[0-9][0-9]*' test.txt

5).查找連續字符范圍{}
使用’.‘和’*'可以設定零個或無限個重復的字符
如果要限制一個范圍則使用’{}'
查看2個o的字符
[root@localhost ~]# grep -n 'o\{2\}' test.txt

查看以’w’開頭,'d’結尾,中間為2,5個o的字串
[root@localhost ~]# grep -n 'wo\{2,5\}d' test.txt

基礎正則運算式常見元字符
| 元字符 | 作用 |
|---|---|
^ | 匹配行首, |
$ | 匹配行尾, |
. | 匹配任意字符 |
\ | 轉義符,屏蔽一個元字符的特殊意義 |
* | 0個或多個*字符之前的那個普通字符 |
[] | 匹配字符集合 |
[^] | 取反 |
\{n,\} | 匹配前面字符至少出現n次 |
\{n,m\} | 匹配前面字符出現n~m次 |
擴展正則運算式----egrep、awk命令支持
擴展正則運算式的常見元字符
| 元字符 | 作用 |
|---|---|
+ | 重復一個或者一個以上的前一個字符 |
? | 零個或者一個的前一個字符 |
| | 零個或者一個的前一個字符 |
() | 查找 “組” 字串 |
()+ | 辨別多個重復的組 |
例如:
[root@localhost ~]# egrep -n 'wo+d' test.txt
執行該命令即可查詢到"wood","woood","wooooood"等字串

[root@localhost ~]# egrep -n 'bes?t' test.txt
執行該命令即可查詢到"bet""best"這兩個字串

[root@localhost ~]# egrep -n 'of|is|on' test.txt
執行該命令即可查詢到"of",或者"is",或者"on"字串

[root@localhost ~]# egrep -n 't(a|e)st' test.txt
"tast"與"test"因為這兩個單詞的"t"與"st"是重復的,所以將"a"與"e"列入"()"符號當中,并以"|"分隔,即可查詢"tast"
或者"test"字串

[root@localhost ~]# egrep -n 'A(xyz)+C' test.txt
該命令是查詢開頭的"A"結尾是"C",中間有一個以上的"xyz"字串的意識

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/57977.html
標籤:其他
上一篇:error C2039: “setAccelerometerKeyHook”: 不是“cocos2d::GLView”的成員
