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命令是一个搜索文件获得模式,使用该命令可以搜索文件中的任意字符串和符号,也可以搜索一个或多个文件的字符串,一个提示符可以是单个字符,一个字符串,一个字或一个句子。

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


相关文章
|
8天前
|
Java 测试技术
Java一分钟之-正则表达式在Java中的应用
【5月更文挑战第14天】正则表达式是Java中用于文本处理的强大力量,通过`java.util.regex`包支持。常见问题包括元字符的理解、边界匹配和贪婪/懒惰量词的使用。错误通常涉及未转义特殊字符、不完整模式或过度匹配。要避免这些问题,需学习实践、使用在线工具和测试调试。示例代码展示了如何验证邮箱地址。掌握正则表达式需要不断练习和调试。
27 2
|
8天前
|
分布式计算 Hadoop Shell
使用shell脚本实现自动SSH互信功能
使用shell脚本实现自动SSH互信功能
19 1
|
8天前
|
Unix Shell Linux
轻松编写 AIX Shell 脚本
轻松编写 AIX Shell 脚本
15 1
|
8天前
|
监控 关系型数据库 Shell
Shell脚本入门:从基础到实践,轻松掌握Shell编程
Shell脚本入门:从基础到实践,轻松掌握Shell编程
|
8天前
|
关系型数据库 MySQL Shell
在Centos7中利用Shell脚本:实现MySQL的数据备份
在Centos7中利用Shell脚本:实现MySQL的数据备份
|
8天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
30 5
|
8天前
|
Shell 程序员 数据安全/隐私保护
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
|
8天前
|
存储 Shell C语言
shell脚本 编程 变量 基本入门(详解)
shell脚本 编程 变量 基本入门(详解)
|
8天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
17 3
|
8天前
|
弹性计算 运维 监控