Shell脚本正则表达式(一)

正则表达式概述

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"字符串的意识

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46902396/article/details/108581567