1. 概述
正則表達式是用來篩選文本的模式模板。將正則表達式同數據相匹配,如果數據與模式一致,那麼就接受處理,如果不一致,就不接受處理。
2. 正則表達式的類型
基本的BRE引擎:
(1)純文本
用標準的文本匹配來處理數據。
如:
[root@localhost chapter17]# echo "this is a test"|sed -n '/test/p'
this is a test
[root@localhost chapter17]# echo "this is a test"|gawk '/test/{print $0}'
this is a test
表達式模式匹配區分大小寫。
(2)定位符
正則表達式認可的特殊的字符有: . * [] ^ $ {} / + ? | ()
如果文本中要匹配這些字符,需要加上轉義字符/
脫字符: ^ 表示文本開頭的模式。
[root@localhost chapter17]# echo "this is a test"|sed -n '/^this/p'
this is a test
[root@localhost chapter17]# echo "^this is a test"|sed -n '/^/^/p'^this is a test
/^ 表示脫字符,匹配以脫字符開頭的行。
(3)結尾符 $
查找以某個字符結尾的行。
[root@localhost chapter17]# cat <data4
this is the first line.
this is the second line.
this is the third line.
this is the fourth test.
[root@localhost chapter17]# sed -n '/test.$/p' data4
this is the fourth test.
(4)點字符
點字符用於匹配除換行符這外的任何單個字符。但點字符必須匹配一個字符。如果點的位置沒有字符,那麼匹配就會失敗。
[root@localhost chapter17]# cat <data6
this is a test of a line.
the cat is sleeping.
this is a very nice hat.
this test is at line four.
at ten clock we'll go home.
[root@localhost chapter17]# sed -n '/.at/p' data6
the cat is sleeping.
this is a very nice hat.
this test is at line four.
解釋一下:
.at匹配以at結尾的字符,但點處必須有一個字符。
顯然第一行沒有at所以不匹配。
第二行cat匹配。
第三行hat匹配。
第四行是 at,即空格+at也匹配。空格也是一個單字符。
第五行前面是換行符即/n+at所以不匹配。
(5)字符類
即可定義一類字符來匹配文本,用方括號來定義字符類。
[root@localhost chapter17]# sed -n '/[ch]at/p' data6
the cat is sleeping.
this is a very nice hat.
即匹配 cat或者是hat.
(6)否定字符類
即不是查找字符類中包括的字符。
用脫字符(^)來表示否定。
[root@localhost chapter17]# sed -n '/[^ch]at/p' data6
this test is at line four.
即查找開頭不以c或h,但結尾以at結束的字符。
(7)星號(*)
星號是表示該字符在匹配模式的文本中不出現或者出現多次。
星號放在字符之後。
[root@localhost chapter17]# echo "tet"|sed -n '/e*/p'
tet
[root@localhost chapter17]# echo "tt"|sed -n '/e*/p'
tt
[root@localhost chapter17]# echo "teeet"|sed -n '/e*/p'
teeet
擴展的BRE引擎: 由於速度等原因,sed只支持基本的BRE引擎,而gawk支持擴展的BRE引擎。
(8)問號(?)
問號表示字符可以不出現或者是出現一次。
[root@localhost chapter17]# echo "teet"|gawk '/te?t/{print $0}'
[root@localhost chapter17]# echo "tet"|gawk '/te?t/{print $0}'
tet
(9)加號(+)
加號其前面的字符可以出現一次或者是多次,但必須出現一次。如果該字符不存在,則不匹配。
[root@localhost chapter17]# echo "bt"|gawk '/be+t/{print $0}'
[root@localhost chapter17]# echo "bet"|gawk '/be+t/{print $0}'
bet
(10)使用大括號
兩種格式:
m表示該正則表達式正好出現m次。
m,n表示該正則表達式最少出現m次,最多出現n次。
[root@localhost chapter17]# echo "bt"|gawk --re-interval '/be{1}t/{print $0}'
[root@localhost chapter17]# echo "bet"|gawk --re-interval '/be{1}t/{print $0}'
bet
[root@localhost chapter17]# echo "beet"|gawk --re-interval '/be{1,2}t/{print $0}'
beet
[root@localhost chapter17]# echo "beeet"|gawk --re-interval '/be{1,2}t/{print $0}'
(11)管道符號|
管道符號表示或者的意思。
[root@localhost chapter17]# echo "the cat is asleep"|gawk '/cat|dog/{print $0}'
3.正則表達式實例
計算目錄文件
#!/bin/bash
#count number of files in your PATH
mypath=`echo $PATH|sed 's/:/ /g'`
count=0
total=0
for dir in $mypath
do
check=`ls $dir`
for item in $check
do
count=$[ $count+1]
done
echo "$dir-$count"
total=$[ $total+$count ]
count=0
done
echo "the total execute file is $total"
結果:
/usr/lib/qt-3.3/bin-13
/usr/kerberos/sbin-14
/usr/kerberos/bin-20
/usr/local/sbin-0
/usr/local/bin-2
/sbin-277
/bin-110
/usr/sbin-513
/usr/bin-2616
/usr/X11R6/bin-2
/opt/real/RealPlayer-12
ls: /root/bin: 沒有那個文件或目錄
/root/bin-0
/opt/real/RealPlayer-12
/opt/real/RealPlayer-12
/opt/real/RealPlayer-12
/opt/real/RealPlayer-12
the total execute file is 3627
就可以計算出可執行文件的個數了。PATH中定義的是可執行文件的路徑。
關於正則表達式就介紹到這裏了。