shell中的简单命令(grep命令和sed 行编辑器)

本文详细介绍了Linux中两个常用的文本处理工具:grep命令和sed行编辑器。grep是一种强大的文本搜索工具,支持正则表达式,通过实例展示了不同匹配条件的使用。sed是一个流编辑器,用于处理纯ASCII码文本,它按行操作,可以进行删除、添加、替换等操作,并提供了多种模式选择。文章还涵盖了sed的各种模式操作,如p、d、a、i、c等,并给出了具体使用案例。
摘要由CSDN通过智能技术生成

1.grep命令

1.Global search regular expression and print out the line(全面搜索研究正则表达示并显示出来)
2.grep 命令是一种强大的文本搜索工具,根据用户指定的“模式”对目标文本进行匹配检查,打印匹配到的行
3.由正则表达示或者字符及基本文本字符所编写的过滤条件
4.grep 的格式
grep 匹配条件 处理文件
例如:
grep root passwd
grep ^root passwd
grep root$ passwd
grep -i root passwd
grep -E “root|ROOT” passwd
5.grep中的正则表达示
^westos
westos$
‘w…s’
'w…
'…s
6.grep正则表达示与扩展正则表达示
正规的grep不支持扩展的正则表达式,竖线是用于表示“或”的扩展正则表达式元字符,正规的grep无法识别,加上反斜杠,这个字就被翻译成扩展正则表达式,就像egrep和grep -E一样

-1-实验:
前提(复制/etc/passwd文件并进行修改):

[root@localhost mnt]# cp /etc/passwd .
[root@localhost mnt]# vim passwd
[root@localhost mnt]# cat passwd

在这里插入图片描述
<1>##过滤passwd文件中的“root”

[root@localhost mnt]# grep root passwd	

在这里插入图片描述
<2>##过滤passed文件中不论大小写的“root”

[root@localhost mnt]# grep -i root passwd

在这里插入图片描述
<3>##过滤passwd文件中root前没有内容的文本

[root@localhost mnt]# grep -i "\<root" passwd

在这里插入图片描述
<4>##过滤passwd文件”root“前后都没有内容的文本

[root@localhost mnt]# grep -i "\<root\>" passwd	

在这里插入图片描述
<5>##过滤passwd文件中“root”后没有内容的文本

[root@localhost mnt]# grep -i "root\>" passwd

在这里插入图片描述
<6>##过滤passwd文件中以“root”开头且“root”后没有内容的文件

[root@localhost mnt]# grep -i "^root\>" passwd

在这里插入图片描述
<7>##过滤passwd文件中以“root”结尾且“root”前没有内容的文件

[root@localhost mnt]# grep -i "\<root$" passwd	

在这里插入图片描述
<8>##过滤passwd文件中以“root”开头或以“root”为结尾的文本

[root@localhost mnt]# egrep -i "^root\>|\<root$" passwd

在这里插入图片描述
<9>##过滤passwd文件中以“root”开头或以“root”为结尾的文本

[root@localhost mnt]# grep -iE "^root\>|\<root$" passwd

在这里插入图片描述
<10>##过滤passwd文件中”root”在中间的文本

[root@localhost mnt]# grep -iE "^root\>|\<root$" passwd -v | grep "root"

在这里插入图片描述
注意:
由结果可知test:roottest:root并没有被过滤出来,原因是因为符合条件的roottest后还有一个root,前者符合条件,后者不符合条件,所以只要有不符合条件的内容,直接会被忽略

<11>##过滤passwd文件中”root”在中间并且前后没有内容的文本
[root@localhost mnt]# grep -iE “^root>|<root$” passwd -v | grep “<root>”
在这里插入图片描述
<12>##显示出passwd文件中的需要的几个字符

[root@localhost mnt]# grep r..t passwd	##过滤passwd文件中以r开头以t结尾的四个字符的内容
[root@localhost mnt]# grep r.... passwd	##过滤passwd文件中以r开头共5个字符的内容
[root@localhost mnt]# grep ....t passwd	##过滤passwd文件中以t结尾共5个字符的内容

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值