shell编程之正则表达式(grep、egrep)

一:正则表达式的定义

正则表达式又称正规表达式、常规表达式。在代码中常简写为 regex、regexp 或 RE。正则表达式是使用单个字符串来描述、匹配一系列符合某个句法规则的字符串,简单来说, 是一种匹配字符串的方法,通过一些特殊符号,实现快速查找、删除、替换某个特定字符串。
正则表达式是由普通字符与元字符组成的文字模式。模式用于描述在搜索文本时要匹 配的一个或多个字符串。正则表达式作为一个模板,将某个字符模式与所搜索的字符串进 行匹配。其中普通字符包括大小写字母、数字、标点符号及一些其他符号,元字符则是指 那些在正则表达式中具有特殊意义的专用字符,可以用来规定其前导字符(即位于元字符 前面的字符)在目标对象中的出现模式。

二:正则表达式用途

正则表达式对于系统管理员来说是非常重要的,系统运行过程中会产生大量的信息,这些信息有些是非常重要的,有些则仅是告知的信息。身为系统管理员如果直接看这么多的信息数据,无法快速定位到重要的信息,这时可以通过正则表达式快速提取“有问题”的信息。如此一来,可以将运维工作变得更加简单方便。

三:基础正则表达式

正则表达式的字符串表达方法根据不同的严谨程度与功能分为基本正则表达式与扩展正则表达式。基础正则表达式是常用正则表达式最基础的部分。在 Linux 系统中常见的文件处理工具中 grep 与 sed 支持基础正则表达式,而 egrep 与 awk 支持扩展正则表达式。

3.1:基础正则表达式案例

3.1.1:查找特定字符

#查找包含'root'字符的行,n:打印行号、i:不区分大小写
[root@server opt]# grep -in 'root' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
#查找不包含'root'的行,v:反向过滤
[root@server opt]# grep -nv 'root' /etc/passwd
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
..........

grep命令的语法格式:
grep [选项] 查找条件 目标文件
常用选项:

  • -c:显示匹配行的数量
  • -i:查找时不区分大小写
  • -v:反转查找
  • -n:显示匹配行的行号
  • -w:匹配单词
  • -o:显示匹配字符串本身

3.1.2:利用中括号“[]”来查找集合字符

提前准备一个名为 test.txt 的测试文件,文件具体内容如下所示:

[root@localhost ~]# cat 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 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.

想要查找“shirt”与“short”这两个字符串时,可以发现这两个字符串均包含“sh”与“rt”。此时执行以下命令即可同时查找到“shirt”与“short”这两个字符串,其中“[]”中无论有几个字符, 都仅代表一个字符,也就是说“[io]”表示匹配“i”或者“o”。

[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!
11:#woood #
12:#woooooood #
14: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. 
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!

3.1.3:查找行首“^”与行尾字符“$”

#查找以‘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 '^[0-9]' test.txt

“^”符号在元字符集合“[]”符号内外的作用是不一样的,在“[]”符号内表示反向选择,在“[]” 符号外则代表定位行首。

#查找以.结尾的行,.是元字符,需加\进行转义
[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. 15:Misfortunes never come alone/single.
16:I shouldn't have lett so tast.

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

3.1.4:查找任意一个字符“.”与重复字符“*”

  • .表示任意一个字符,除了\n
  • *表示前面一个字符出现0次或者多次
[root@server ~]# 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@server ~]# 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!
11:#woood # 
12:#woooooood # 
14:I bet this place is really spooky late at night! 
#查询以 w 开头 d 结尾,中间包含至少一个 o 的字符串
[root@server ~]# grep -n 'woo*d' test.txt 
8:a wood cross!
11:#woood # 
12:#woooooood # 
#查询以 w 开头 d 结尾,中间的字符可有可无的字符串
[root@server ~]# 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
11:#woood # 
12:#woooooood # 
#查询任意数字所在行
[root@server ~]# grep -n '[0-9][0-9]*' test.txt 
4:the tongue is boneless but it breaks bones.12! 
7:PI=3.141592653589793238462643383249901429

3.1.5:查找连续字符范围“{}”

因为“{}”在 Shell 中具有特殊意义,所以在使用“{}”字符时,需要利用转义字符“\”,将“{}”字符转换成普通字符。

#查找2个o的字符, \{n\}:表示匹配前面的字符n次
[root@server ~]# 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!
11:#woood # 
12:#woooooood # 
14:I bet this place is really spooky late at night! 
#查询以 w 开头以 d 结尾,中间包含 2~5 个 o 的字符串,\{n,m\}:表示匹配前一个字符n到m次
[root@server ~]# grep -n 'wo\{2,5\}d' test.txt 
8:a wood cross!
11:#woood # 
#查询以 w 开头以 d 结尾,中间包含 2 个或 2 个以上 o 的字符串,\{n,\}:表示匹配前一个字符重复出现至少n次
[root@server ~]# grep -n 'wo\{2,\}d' test.txt 
8:a wood cross!
11:#woood # 
12:#woooooood # 

四:基础正则表达式与字符总结

元字符作用
^匹配输入字符串的开始位置。除非在方括号表达式中使用,表示不包含该字符集合。要匹配“^” 字符本身,请使用“\ ^”
$匹配输入字符串的结尾位置。要匹配“$”字符本身,请使用“\ $”
.匹配除“\r\n”之外的任何单个字符
\反斜杠,又叫转义字符,去除其后紧跟的元字符或通配符的特殊意义
*匹配前面的子表达式零次或多次。要匹配“ * ”字符,请使用“\ *”
[]字符集合。匹配所包含的任意一个字符。
[^]赋值字符集合。匹配未包含的一个任意字符。
[n1-n2]字符范围。匹配指定范围内的任意一个字符。如 [a-z]
\ {n\ }n 是一个非负整数,匹配确定的 n 次。
\ {n,\ }n 是一个非负整数,至少匹配 n 次。
\ {n,m\ }m 和 n 均为非负整数,其中 n<=m,最少匹配 n 次且最多匹配m 次

五:扩展正则表达式

与基础正则表达式类型相同,扩展正则表达式也包含多个元字符,常见的扩展正则表达 式的元字符主要包括以下几个:

元字符作用
+重复一个或者一个以上的前一个字符
零个或者一个的前一个字符
使用或者(or)的方式找出多个字符
()查找“组”字符串
()+辨别多个重复的组

如果使用扩展正则表达式,需要使用 egrep命令,egrep 命令与 grep 命令的用法基本相似。egrep 命令是一个搜索文件获得模式,使用该命令可以搜索文件中的任意字符串和符号,也可以搜索一个或多个文件的字符串,一个提示符可以是单个字符、一个字符串、一个字或一个句子。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值