Shell编程之正则表达式与文本处理器

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
\r表示回车 \n表示换行
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
只显示匹配的字符串
grep -o
-v是取反的意思

[root@localhost ~]# grep -n 'the' 10.txt  //显示有the的行,并且显示行号
3:the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4:The year ahead will test our political establishment to the limit.

a

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

在这里插入图片描述

[root@localhost ~]# grep -in 'the' 10.txt    //不区分大小写过滤
2:He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3:the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4:The year ahead will test our political establishment to the limit.

在这里插入图片描述
在这里插入图片描述

[root@localhost ~]# grep -n 'sh[io]rt' 10.txt
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.

在这里插入图片描述

[root@localhost ~]# grep -n 'oo' 10.txt  
2:He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3:the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
6:a wood cross!
10:#woood # #woooooood # AxyzxyzxyzxyzC
11:I bet this place is really spooky late at night! Misfortunes never come alone/single.

在这里插入图片描述

[root@localhost ~]# grep -n '[^w]oo' 10.txt   //grep命令中的^不是开头的意思,而是取反的意思
2:He was wearing a blue polo shirt with black pants. The home ofFootball on BBC Sport online.
3:the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
10:#woood # #woooooood # AxyzxyzxyzxyzC
11:I bet this place is really spooky late at night! Misfortunes never come alone/single.

在这里插入图片描述

[root@localhost ~]# grep -n 'woo*d' 10.txt
6:a wood cross!
10:#woood # #woooooood # AxyzxyzxyzxyzC

在这里插入图片描述

[root@localhost ~]# grep -n 'w.*d' 10.txt  //.的意思是匹配一个字符  ..是匹配两个字符  *重复前面字符0次或者多个前面的单字符
1:he was short and fat.
3:the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
6:a wood cross!
7:Actions speak louder than words
10:#woood # #woooooood # AxyzxyzxyzxyzC

在这里插入图片描述
在这里插入图片描述

[root@localhost ~]# grep -n '[0-9][0-9]*' 10.txt
3:the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
5:PI=3.141592653589793238462643383249901429

在这里插入图片描述
在这里插入图片描述

[root@localhost ~]# grep -n 'o\{2\}' 10.txt
2:He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3:the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
6:a wood cross!
10:#woood # #woooooood # AxyzxyzxyzxyzC
11:I bet this place is really spooky late at night! Misfortunes never come alone/single.

在这里插入图片描述
在这里插入图片描述

[root@localhost ~]# grep -n 'wo\{2,5\}d' 10.txt
6:a wood cross!
10:#woood # #woooooood # AxyzxyzxyzxyzC

[root@localhost ~]# grep -n 'wo\{2,\}d' 10.txt
6:a wood cross!
10:#woood # #woooooood # AxyzxyzxyzxyzC

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

+的作用

[root@localhost ~]# egrep -n 'wo+d' 10.txt
6:a wood cross!
10:#woood # #woooooood # AxyzxyzxyzxyzC

在这里插入图片描述
?的作用

[root@localhost ~]# egrep -n 'bes?t' 10.txt
3:the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
11:I bet this place is really spooky late at night! Misfortunes never come alone/single.

在这里插入图片描述
|的作用

[root@localhost ~]# egrep -n 'of|is|on' 10.txt
2:He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3:the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4:The year ahead will test our political estabishment to the limit.
7:Actions speak louder than words
11:I bet this place is really spooky late at night! Misfortunes never come alone/single.

在这里插入图片描述
()的作用

[root@localhost ~]# egrep -n 't(a|b)st' 11.txt
13:I shouldn't have lett so tast.

在这里插入图片描述
()+的作用

[root@localhost ~]# egrep -n 'A(xyz)+C' 10.txt
10:#woood # #woooooood # AxyzxyzxyzxyzC

Sed工具概述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

 [root@localhost ~]# sed -n 'p' 10.txt    //输出所有内容,等同于   cat 10.txt

在这里插入图片描述

[root@localhost ~]# sed -n '3p' 10.txt   //输出第3行

在这里插入图片描述

[root@localhost ~]# sed -n '3p,5p' 10.txt   //输出3~5行

在这里插入图片描述

[root@localhost ~]# sed -n 'p;n' 10.txt    //输出所有奇数行,n表示读入下一行资料

在这里插入图片描述
[root@localhost ~]# sed -n ‘n;p’ 10.txt //输出所有偶数行,n表示读入下一行资料
在这里插入图片描述
在这里插入图片描述

[root@localhost ~]# sed -n '1,5{p;n}' 10.txt   //输出第 1~5行之间的奇数行 (第1 ,3  ,5行)

在这里插入图片描述

[root@localhost ~]# sed -n '10,${n;p}' 10.txt  //输出第10行至文件尾之间的偶数行  (注意10行视为1 然后往下依次排序 )

在这里插入图片描述
在这里插入图片描述

[root@localhost ~]# sed -n '/the/p' 10.txt      //输出包含the 的行

在这里插入图片描述

[root@localhost ~]# sed -n'4,/the/p' 10.txt   //输出从第4行至第一个包含the的行
从第四行开始找the,第四行也输出出来,顺带把没有the的行也全打印出来了直到遇到第一个the的行停止

在这里插入图片描述
在这里插入图片描述

[root@localhost ~]# sed -n '/the/=' 10.txt     //输出包含the的行所在的行号,等号(=)用来输出行号

在这里插入图片描述

[root@localhost ~]# sed -n '/^PI/p' 10.txt     //输出以PI开头的行

在这里插入图片描述

[root@localhost ~]# sed -n '/[0-9]$/p' 10.txt  //输出以数字结尾的行

在这里插入图片描述

[root@localhost ~]# sed -n '/\<wood\>/p' 10.txt  //输出包含单词wood的行

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值