Shell的基础正则表达式详解

正则表达式的定义

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

正则表达式的用途

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

正则表达式的分类

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

基础正则表达式元字符

在这里插入图片描述

以一篇文章为例,对这篇文章进行grep和sed的操作

在这里插入图片描述

grep的使用规则

  • n 表示显示行号
  • -i 表示不区分大小写
  • -v 表示反向过滤
  • -[ ] 查找集合字符

grep的操作(本次所有操作基于test.txt进行操作)

[root@localhost~]# grep -n 'the' test.txt    #包含"the"的会被带行号显示出来,-n显示行号

在这里插入图片描述

[root@localhost~]# grep -vn 'the' test.txt   #不包含"the"的会被带行号显示出来

在这里插入图片描述

[root@localhost~]# grep -n 'sh[io]rt' test.txt    #以sh开头rt结尾中间有i或o的显示出来

在这里插入图片描述

[root@localhost~]# grep -n 'oo' test.txt
[root@localhost~]# grep -n 'o\{2\}' test.txt 
[root@localhost~]# grep -n 'o\{2,\}' test.txt   #包含"oo"的带行号显示出来,三种写法都可以

在这里插入图片描述

[root@localhost~]# grep -n 'o\{2,5\}' test.txt     #包含"2-5个的o"带行号显示出来

在这里插入图片描述

[root@localhost~]# grep -n '[^w]oo' test.txt       #查找"oo"前面不是"w的字符串"

在这里插入图片描述

[root@localhost~]# grep -n '[^a-z]oo' test.txt      #过滤"oo"前面是小写的

在这里插入图片描述

[root@localhost~]# grep -n '[0-9]' test.txt         #查找包含数字的行

在这里插入图片描述

[root@localhost~]# grep -n '[^0-9]' test.txt        #过滤包含纯数字的行

在这里插入图片描述

[root@localhost~]# grep -n '^the' test.txt            #查找以"the"开头的行

在这里插入图片描述

[root@localhost~]# grep -n '^[a-z]' test.txt          #查找以小写字母开头的行

在这里插入图片描述

[root@localhost~]# grep -n '^[A-Z]' test.txt          #查找以大写字母开头的行

在这里插入图片描述

[root@localhost~]# grep -n '^[^a-zA-Z]' test.txt     #查询不以字母开头的行

在这里插入图片描述

[root@localhost~]# grep -n '\.$' test.txt            #查询以"."结尾的行

在这里插入图片描述

[root@localhost~]# grep -n '^$' test.txt             #查询空白行

在这里插入图片描述

[root@localhost~]# grep -n 'w..d' test.txt       #查询以"w开头d结尾中间两个字符"的行

在这里插入图片描述

[root@localhost~]# grep -n 'ooo*' test.txt 
#查询两个以上的"o"的行,"o*"表示拥有零个或大于等于一个"o"的字符,因为允许空字符,所以执行

在这里插入图片描述

[root@localhost~]# grep -n 'w.*d' test.txt    #查询以w开头d结尾,中间字符可有可无的字符串

在这里插入图片描述

[root@localhost~]#  grep -n '[0-9][0-9]*' test.txt     #查询包含数字,两位数或者一位数

在这里插入图片描述

sed常见的操作包括以下几种

  • a:增加,在当前行下面增加一行指定内容
  • c:替换,将选定行替换为指定内容
  • d:删除,删除选定的行
  • i:插入,在选定行上面插入一行指定内容
  • p:打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以ASCII码输出。其通常与“-n”选项一起使用
  • s:替换,替换指定字符
  • y:字符转换

sed的输出操作(本次所有操作基于test.txt进行操作)

[root@localhost~]# nl test.txt | sed -n 'p'        #输出全文本,"p"表示正常输出
[root@localhost~]# nl test.txt | sed -n '3p'       #输出指定行:'xp''3p'输出第三行
[root@localhost~]# nl test.txt | sed -n '2,6p'     #输出指定行到指定行,'2,6p'输出2-6[root@localhost~]# nl test.txt | sed -n 'p;n'      #输出奇数行,'p;n'是输出奇数行
[root@localhost~]# nl test.txt | sed -n 'n;p'      #输出偶数行,'n;p'是输出偶数行
[root@localhost~]# nl test.txt | sed -n '1,10{p;n}'#输出某行到某行之间的奇数行
[root@localhost~]# nl test.txt | sed -n '1,10{n;p}'#输出某行到某行之间的偶数行
[root@localhost~]# nl test.txt | sed -n '12,${n;p}'#输出某行到结尾的偶数行,$:结尾
[root@localhost~]# nl test.txt | sed -n '/the/p'   #输出包含'the'的行
[root@localhost~]# nl test.txt | sed -n '4,/the/p' #输出从某行至结尾第一个包含'the'的行
[root@localhost~]# nl test.txt | sed -n '/the/='   #输出包含'the'的行,'='只输出行号
[root@localhost~]# sed -n '/^PI/p' test.txt        #输出以PI开头的行
[root@localhost~]# nl test.txt | sed -n '/[0-9]$/p'#输出以数字结尾的行
[root@localhost~]# nl test.txt | sed -n '/\<wood\>/p'
#输出包含单词'wood'的行,/\<、\>代表单词边界

sed的删除操作(本次所有操作基于test.txt进行操作)

[root@localhost~]# nl test.txt | sed '3d'          #删除某行,'d'删除 文本内容没有做出更改
[root@localhost~]# nl test.txt | sed '3,6d'        #删除某行到某行(3-6行)
[root@localhost~]# nl test.txt | sed '/cross/d'    #删除包含'cross'的行
[root@localhost~]# nl test.txt | sed '/cross/!d'   #删除不包含'cross'的行,只有第八行保留
[root@localhost~]# sed '/^[a-z]/d' test.txt        #删除小写字母开头的行,只剩下大写开头的
[root@localhost~]# sed '/\./d' test.txt            #删除'.'结尾的行
[root@localhost~]# sed '/^$/d' test.txt            #删除所有空行

sed的替换操作(本次所有操作基于test.txt进行操作)

[root@localhost~]#sed 's/the/THE/’ test.txt        #将每行中的第一个 the替换为THE
[root@localhost~]#sed 's/1/L/2’ test.txt           #将每行中的第2个l替换为L
[root@localhost~]#sed 's/the/THE/g' test.txt       #将文件中的所有 the替换为 THE
[root@localhost~]#sed 's/o//g' test.txt            #将文件中的所有o删除(替换为空串)
[root@localhost~]#sed 's/^/#/’ test.txt            #在每行行首插入#号
[root@localhost~]#sed '/the/s/^ /#/’ test.txt      #在包含the 的每行行首插入#号
[root@localhost~]#sed's/$/EOF/' test.txt           #在每行行尾插入字符串 EOF
[root@localhost~]#sed '3,5s/the/THE/g' test.txt    #将第3~5行中的所有the替换为THE                                                                   
[root@localhost~]#sed '/the/s/o/0/g' test.txt      #将包含the 的所有行中的o都替换为0                                                            

sed的迁移操作(本次所有操作基于test.txt进行操作)

在使用sed 命令迁移符合条件的文本时,常用到以下参数.

  • H:复制到剪贴板
  • g、G:将剪贴板中的数据覆盖/追加至指定行
  • w:保存为文件
  • r:读取指定文件
  • a:追加指定内容
sed '/the/{H; d} ;$G' test.txt         #将包含the 的行迁移至文件末尾,{;}用于多个操作   
sed '1,5H;d};17G' test.txt             #将第1~5 行内容转移至第17行后
sed '/the/w out.file' test.txt         #将包含 the 的行另存为文件 out.file
sed '/the/r /etc/hostname' test. txt   #将文件/etc/hostname 的内容添加到包含the的每行以后                                                       
sed '$aNew' test.txt                   #在最末尾后插入一个新行,内容为NEW
sed '3aNew' test.txt                   #在第3 行后插入一个新行,内容为New
sed '/the/aNew' test.txt               #在包含 the的每行后插入一个新行,内容为New                                      
sed '3aNew1\nNew2' test.txt            #在第3行后插入多行内容,中间的\n表示换行                                                                        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值