
本篇主要寫一些shell腳本正則運算式的使用基礎,
概述
正則運算式分為基礎正則運算式(Regular Expression)與擴展正則運算式(Extended Regular Expression),
它不是一個工具程式,而是一個字串處理的標準依據,是使用單個字串搜索、匹配一系列符合某個語法規則的字串,
它由普通字符(a~z)以及特殊字符(元字符)組成,
linux 文本處理工具
| 文本處理工具 | 基礎正則運算式 | 擴展正則運算式 |
|---|---|---|
| vi編輯器 | 支持 | / |
| grep | 支持 | / |
| egrep | 支持 | 支持 |
| sed | 支持 | / |
| awk | 支持 | 支持 |
基礎正則運算式
- 基礎正則運算式常見元字符
^:匹配輸入字串的開始位置,在方括號運算式中使用,表示不包含該字符集合,要匹配^字符本身,使用\^
$:匹配輸入字串的結尾位置,如果設定了RegExp物件的Multiline屬性,則$也匹配\n或\r,要匹配$字符本身,請使用\$
.:匹配除\r\n之外的任何單個字符
\:將下一個字符標記為特殊字符、原義字符、向后參考、八進制轉義符,例如,n匹配字符n, \n匹配換行符,序列\\匹配\,而\(則匹配(
*:匹配前面的子運算式零次或多次,要匹配*字符,請使用\*
[]:字符集合,匹配所包含的任意一個字符,例如,[abc]可以匹配plain中的a
[^]:賦值字符集合,匹配未包含的一個任意字符,例如,[^abc]可以匹配plain中plin中的任何一個字母
[n1-n2]:字符范圍,匹配指定范圍內的任意一個字符,例如,[a-z]可以匹配a到z范圍內的任意一個小寫字母字符,
{n}:n是一個非負整數,匹配確定的n次,例如,o{2}不能匹配Bob中的o,但是能匹配food中的兩個o
{n,}:n是一個非負整數,至少匹配n次,例如,o{2,}不能匹配Bob中的o,但能匹配foooood中的所有o,o{1,}等價于o+,o{0,}則等價于o*
{n,m}:m和n均為非負整數,其中n<=m,最少匹配n次且最多匹配m次
- 使用
grep做示例,首先準備一個測驗檔案
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.
特定字符
-n:顯示行號
-i:不區分大小寫
-v:反向選擇
[root@localhost ~]# grep -n 'the' test.txt
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@localhost ~]# grep -in 'the' test.txt
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@localhost ~]# grep -vn 'the' 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.
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.
集合“[]”
- 中括號“[]”中無論又幾個字符,都只匹配其中一個
[root@localhost ~]# grep -n 'sh[io]rt' test.txt
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
- 匹配重復單個字符
oo
[root@localhost ~]# grep -n 'oo' test.txt
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!
- 查找
oo前不是w的字串
[root@localhost ~]# grep -n '[^w]oo' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
12:#woood #
13:#woooooood #
15:I bet this place is really spooky late at night!
- 查找
oo前沒有小寫字母的字串
[root@localhost ~]# grep -n '[^a-z]oo' test.txt
3:The home of Football on BBC Sport online.
- 查找包含數字的行
[root@localhost ~]# grep -n '[0-9]' test.txt
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429
行首“^”
- 查找已
the為行首的行
[root@localhost ~]# grep -n '^the' test.txt
4:the tongue is boneless but it breaks bones.12!
- 查找是小寫字母開頭的行
[root@localhost ~]# grep -n '^[a-z]' test.txt
1:he was short and fat.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
8:a wood cross!
- 查找是大寫字母開頭的行
[root@localhost ~]# grep -n '^[A-Z]' test.txt
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
6:The year ahead will test our political establishment to the limit.
7:PI=3.141592653589793238462643383249901429
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@localhost ~]# grep -n '^[^a-zA-Z]' test.txt
12:#woood #
13:#woooooood #
行尾“$”
- 查找以
.結尾的行,需要使用轉義字符
[root@localhost ~]# grep -n '\.$' 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.
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@localhost ~]# grep -n '^$' test.txt
10:
11:
任意一個字符“.”
- 查找
w和d中間有兩個字符的字串
[root@localhost ~]# grep -n 'w..d' test.txt
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
重復字符“*”
- 查找至少兩個以上的
o的字串,*代表重復前面的一個字符零個或多個
[root@localhost ~]# grep -n 'ooo*' test.txt
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!
- 查找
w和d中間有兩個至少有一個o的字串
[root@localhost ~]# grep -n 'woo*d' test.txt
8:a wood cross!
12:#woood #
13:#woooooood #
- 查找
w和d中間可有可無的字串
[root@localhost ~]# grep -n 'w.*d' test.txt
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@localhost ~]# grep -n '[0-9][0-9]*' test.txt
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429
連續字符范圍“{}”
- 查找有連續兩個
o的字串,需要轉義
[root@localhost ~]# grep -n 'o\{2\}' test.txt
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!
- 查找
w和d中間包含2~5個o的字串
[root@localhost ~]# grep -n 'wo\{2,5\}d' test.txt
8:a wood cross!
12:#woood #
- 查找
w和d中間包含兩個以上o的字串
[root@localhost ~]# grep -n 'wo\{2,\}d' test.txt
8:a wood cross!
12:#woood #
13:#woooooood #
擴展正則運算式
- 擴展正則運算式常見元字符
+:重復一個或者一個以上的前一個字符
?:零個或者一個的前一個字符
|:使用或者or的方式找出多個字符
():查找組字串
()+:辨別多個重復的組
- 使用
rgrep做示例,查詢w和d之間包含一個以上o的字串
[root@localhost ~]# egrep -n 'wo+d' test.txt
8:a wood cross!
12:#woood #
13:#woooooood #
- 查詢
bet和best這兩個字串
[root@localhost ~]# egrep -n 'bes?t' test.txt
5:google is the best tools for search keyword.
15:I bet this place is really spooky late at night!
- 查詢
of或者if或者on字串
[root@localhost ~]# egrep -n 'of|is|on' test.txt
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.
9:Actions speak louder than words
15:I bet this place is really spooky late at night!
16:Misfortunes never come alone/single.
- 查詢
tast或者test字串
[root@localhost ~]# egrep -n 't(a|e)st' test.txt
6:The year ahead will test our political establishment to the limit.
17:I shouldn't have lett so tast.
- 查詢開頭的
A結尾是C,中間有一個以上的xyz字串
[root@localhost ~]# egrep -n 'A(xyz)+C' test.txt
14:AxyzxyzxyzxyzC
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/162670.html
標籤:Linux
