shell脚本应用——正则表达式

简介: shell脚本应用——正则表达式

正则表达式

  • 正则表达式的定义

       正则表达式又称为正规表达式、常规表达式。在代码中常简写为regex、regexp或RE。

       是使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。

       正则表达式的组成:普通字符(大小写字母、数字、标点符号及一些其他符号) 元字符(在正则表达式中具有特殊意义的专用字符)

       正则表达式一般用于脚本编程与文本编辑器中。很多文本处理器与程序设计语言均支持正则表达式,Linux系统中常见的文本处理器(grep,egrep,sed,awk)。正则表达式具备很强大的文本匹配功能,能够在文本海洋中快速高效地处理文本。

  • 正则表达式用途

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

       目前很多软件也支持正则表达式,最常见的就是邮件服务器。除了邮件服务器只外,很多服务器软件都支持正则表达式。虽然这些软件都支持正则表达式,不过字符串的对比规则还需要系统管理员来添加,所以作为系统管理员,正则表达式是必须掌握的技能之一。

基础正则表达式

       正则表达式的字符串表达方法根据不同的严谨程度与功能分为基础正则表达式与扩展正则表达式。基础正则表达式是常用的正则表达式是常用的正则表达式的最基础的部分。在linux系统中常见的文本处理工具中grep与sed支持基础正则表达式,而egrep与awk支持扩展正则表达式。掌握基础正则表达式的使用方法,首先必须了解基本正则表达式所包含的元字符的含义,下面通过grep命令以列举的方式逐个介绍。

基础正则表达式示例

       下面操作需要提前准备一个名为test.txt的测试文件,文件具体内容如下。

1.  [root@localhost ~]# cat test.txt
2.  the tongue is boneless but it breaks bones.12!
3.  google is the best tools for search keyword.
4.  The year ahead will test our political establishment to the limit.
5.  PI=3.141592653589793238462643383249901429
6.  a wood cross!
7.  Actions speak louder than words
8.  
9.  #woood #
10.  #woooooood #
11.  AxyzxyzxyzxyzC
12.  I bet this place is really spooky late at night!
13.  Misfortunes never come alone/single.
14.  I shouldn't have lett so tast.

查找特定字符

       查找特定字符非常简单,如执行以下命令即可从test.txt文件中查找特定字符“the”所在位置。其中“-n”表示显示行号,“-i” 表示不区分大小写。命令执行后,符合匹配标准的字符,字体颜色会变成红色。

1.  [root@localhost ~]# grep -n 'the' test.txt 
2.  4:the tongue is boneless but it breaks bones.12!
3.  5:google is the best tools for search keyword.
4.  6:The year ahead will test our political establishment to the limit.
5.  [root@localhost ~]# grep -in 'the' test.txt 
6.  3:The home of Football on BBC Sport online.
7.  4:the tongue is boneless but it breaks bones.12!
8.  5:google is the best tools for search keyword.
9.  6:The year ahead will test our political establishment to the limit.

       若反向选择,股查找不包含“the”字符的行,则需要通过grep命令的“-vn”选项实现。

1.  [root@localhost ~]# grep -vn 'the' test.txt 
2.  1:he was short and fat.
3.  2:He was wearing a blue polo shirt with black pants.
4.  3:The home of Football on BBC Sport online.
5.  7:PI=3.141592653589793238462643383249901429
6.  8:a wood cross!
7.  9:Actions speak louder than words
8.  10:
9.  11:#woood #
10.  12:#woooooood #
11.  13:AxyzxyzxyzxyzC
12.  14:I bet this place is really spooky late at night!
13.  15:Misfortunes never come alone/single.
14.  16:I shouldn't have lett so tast.

中括号“[]”来查找集合字符

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

1.  [root@localhost ~]# grep -n 'sh[io]rt' test.txt
2.  1:he was short and fat.
3.  2:He was wearing a blue polo shirt with black pants.

       若要查找包含重复单个字符“oo”时,只需要执行以下命令即可。

1.  [root@localhost ~]# grep -n 'oo' test.txt
2.  3:The home of Football on BBC Sport online.
3.  5:google is the best tools for search keyword.
4.  8:a wood cross!
5.  11:#woood #
6.  12:#woooooood #
7.  14:I bet this place is really spooky late at night!

       若查找“oo”前面不是“w”的字符串,只需要通过集合字符的反方向选择“[^]”来实现该目的,如执行“grep -n 'woo' test.txt”命令表示在test.txt文本中查找“oo”前面不是“w”的字符串。

1.  [root@localhost ~]# grep -n '[^w]oo' test.txt
2.  3:The home of Football on BBC Sport online.
3.  5:google is the best tools for search keyword.
4.  11:#woood #
5.  12:#woooooood #
6.  14:I bet this place is really spooky late at night!

       在上述命令的执行结果中发现“woood”与“woooooood”也符合匹配规则,二者均包含“w”。其实通过执行结果就可以看出,符合匹配标准的字符加粗显示,而上述结果中可以得知,“#woood#”中加粗显示的是“ooo”,而“oo”前面的“o”是符合匹配规则的。同理“#woooooood#”也符合匹配规则。

若不希望“oo”前面存在小写字母,可以使用“grep -n '[ ^a-z]oo' test.txt”命令实现,其中“a-z”表示小写字母,大写字母则通过“A-Z”表示。

1.  [root@localhost ~]# grep -n '[^a-z]oo' test.txt
2.  3:The home of Football on BBC Sport online.

查找包含数字的行可以通过“grep -n '[0-9]' test.txt”命令来实现。

1.  [root@localhost ~]# grep -n '[0-9]' test.txt
2.  4:the tongue is boneless but it breaks bones.12!
3.  7:PI=3.141592653589793238462643383249901429

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

       基础正则表达式包含两个定位元字符:"^"(行首)与"$"(行尾)。在上面的示例中,查询“the”字符串时出现了很多包含”the“的行,如果想要查询以”the“字符串为首的行,则可以通过”^“元字符来实现。

1.  [root@localhost ~]# grep -n "^the" test.txt 
2.  4:the tongue is boneless but it breaks bones.12!

       查询以小写字母开头的行可以通过“^[a-z]”规则来过滤,查询大写字母开头的行则使用“^[A-Z]”规则,若查询不以字母开头的行则使用“^[ ^a-zA-Z]”规则。

1.      [root@localhost ~]# grep -n "^[a-z]" test.txt 
2.  1:he was short and fat.
3.  4:the tongue is boneless but it breaks bones.12!
4.  5:google is the best tools for search keyword.
5.  8:a wood cross!
6.      [root@localhost ~]# grep -n "^[A-Z]" test.txt 
7.  2:He was wearing a blue polo shirt with black pants.
8.  3:The home of Football on BBC Sport online.
9.  6:The year ahead will test our political establishment to the limit.
10.  7:PI=3.141592653589793238462643383249901429
11.  9:Actions speak louder than words
12.  13:AxyzxyzxyzxyzC
13.  14:I bet this place is really spooky late at night!
14.  15:Misfortunes never come alone/single.
15.  16:I shouldn't have lett so tast.
16.      [root@localhost ~]# grep -n "^[^a-zA-Z]" test.txt 
17.  11:#woood #
18.  12:#woooooood #

       "^"符号在元字符集合“[]”符号内外的作用是不一样的,在"[]"符号内表示反方向选择,在"[]"符号外表示定位首行。反之,若想查找以某一特定字符结尾的行则可以使用"$"定位符。例如,执行以下命令既可以实现查询以小数点(.)结尾的行。因为小数点在正则表达式中也是一个元字符,所以需要使用转义字符" \ "将具有特殊意义的字符转化成普通字符。

1.  [root@localhost ~]# grep -n "\.$" test.txt 
2.  1:he was short and fat.
3.  2:He was wearing a blue polo shirt with black pants.
4.  3:The home of Football on BBC Sport online.
5.  5:google is the best tools for search keyword.
6.  6:The year ahead will test our political establishment to the limit.
7.  15:Misfortunes never come alone/single.
8.  16:I shouldn't have lett so tast.

       当查询空白行时,执行"grep -n "^$" test.txt "命令即可。

1.  [root@localhost ~]# grep -n "^$" test.txt 
2.  10:

查找任意一个字符"."与重复字符"*"

       在正则表达式中小数点代表任意一个字符,例如,执行以下命令就可以查找“w??d”的字符串,即共有四个字符,以w开头d结尾。

1.  [root@localhost ~]# grep -n "w..d" test.txt 
2.  5:google is the best tools for search keyword.
3.  8:a wood cross!
4.  9:Actions speak louder than words

       在上述结果中,“wood”字符串“w..d”匹配规则。若想要查询oo,ooo,ooooo等资料。则需要使用星号(*)元字符。但需要注意的是,“ * ”代表的是重复零个或多个前面的单字符。“o*”表示拥有零个(即为空字符)或大于等于一个“o"的字符,因为允许空字符,所以执行“grep -n "o*" test.txt”命令会将文本中所有的内容都输出打印。如果是“oo*”,则第一个o必须存在,第二个o则是零个或多个o,所以凡是包含o,oo,ooo等的资料都符合标准。若查询包含至少两个o以上的字符串,则执行“grep -n "ooo*" test.txt”命令即可。注:"ooo*"表示内容为oo,第三个o为查找的字母,o*可以是零个也可以是n个。

1.  [root@localhost ~]# grep -n "ooo*" test.txt
2.  3:The home of Football on BBC Sport online.
3.  5:google is the best tools for search keyword.
4.  8:a wood cross!
5.  11:#woood #
6.  12:#woooooood #
7.  14:I bet this place is really spooky late at night!

       查询以w开头d结尾,中间至少包含一个o的字符串,执行以下命令即可。

1.  [root@localhost ~]# grep -n "woo*d" test.txt 
2.  8:a wood cross!
3.  11:#woood #
4.  12:#woooooood #

       查询以w开头d结尾,中间的字符可有可无的字符串。

1.  [root@localhost ~]# grep -n "w.*d" test.txt 
2.  1:he was short and fat.
3.  5:google is the best tools for search keyword.
4.  8:a wood cross!
5.  9:Actions speak louder than words
6.  11:#woood #
7.  12:#woooooood #

       查询任意数字所在行。

1.  [root@localhost ~]# grep -n "[0-9][0-9]*" test.txt 
2.  4:the tongue is boneless but it breaks bones.12!
3.  7:PI=3.141592653589793238462643383249901429.

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

       在上面的示例中,我们使用“.”与“*”来设定零个到无限个重复的字符,如果想要限制一个范围内的重复的字符串该如何实现?例如,查找三到五个o的连续字符,这个时候就需要使用基础正则表达式中的限定范围的字符“{}”。因为在shell中具有特殊意义,所以在使用“{}”字符时,需要利用转义字符“\”将“{}”字符转换成普通字符。“{}”字符的使用方法如下。

(1)查询两个o的字符

1.  [root@localhost ~]# grep -n "o\{2\}" test.txt
2.  3:The home of Football on BBC Sport online.
3.  5:google is the best tools for search keyword.
4.  8:a wood cross!
5.  11:#woood #
6.  12:#woooooood #
7.  14:I bet this place is really spooky late at night!

(2)查询以w开头以d结尾,中间包含2~5个o的字符串。

1.  [root@localhost ~]# grep -n "wo\{2,5\}d" test.txt 
2.  8:a wood cross!
3.  11:#woood #

(3)查询以w开头以d结尾,中间包含2个以上o的字符串

1.  [root@localhost ~]# grep -n "wo\{2,\}d" test.txt 
2.  8:a wood cross!
3.  11:#woood #
4.  12:#woooooood #

元字符总结

扩展正则表达式

       通常情况下会使用基础正则表达式就已将够了,但是有时为了简化整个指令,需要使用范围更广的扩展正则表达式。例如,使用基础正则表达式查询除文件中空白行与行首为“#”之外的行,(通常用于查看生效的配置文件),执行"grep -v "^$" test.txt | grep -v "^#" “即可实现。这里需要使用管道命令来搜索两次。如果使用扩展正则表达式,可以简化为"egrep -v "^$|^$" test.txt",其中,双引号内的管道符号表示或者(or)

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

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


相关文章
|
27天前
|
弹性计算 Shell Perl
ecs服务器shell常用脚本练习(二)
【4月更文挑战第1天】shell代码训练(二)
106 1
|
29天前
|
Java Shell
SpringBoot启动脚本Shell
SpringBoot启动脚本Shell
18 0
|
7天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
36 3
|
2天前
|
弹性计算 运维 Shell
每天解析一个shell脚本(61)
【4月更文挑战第26天】shell脚本解析及训练(61)
14 3
|
2天前
|
弹性计算 运维 Shell
每天解析一个shell脚本(58)
【4月更文挑战第26天】shell脚本解析及训练(58)
68 0
|
2天前
|
弹性计算 Shell 数据安全/隐私保护
每天解析一个shell脚本(56)
【4月更文挑战第26天】shell脚本解析及训练(56)
14 0
|
4天前
|
监控 Shell 应用服务中间件
第十二章 Shell脚本编写及常见面试题(二)
第十二章 Shell脚本编写及常见面试题(二)
|
4天前
|
监控 关系型数据库 Shell
第十二章 Shell脚本编写及常见面试题(一)
第十二章 Shell脚本编写及常见面试题(一)
|
4天前
|
监控 Shell
生产环境Shell脚本Ping监控主机是否存活(多种方法)
生产环境Shell脚本Ping监控主机是否存活(多种方法)
|
4天前
|
运维 Shell
Shell脚本判断IP是否合法性(多种方法)
Shell脚本判断IP是否合法性(多种方法)