Linux shell编程之正则表达式

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中定义的是可执行文件的路径。

 

 

关于正则表达式就介绍到这里了。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值