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一样

实验前提:复制/etc/passwd文件到当前路径下并进行修改

[root@shell_example ~]# cd /mnt/
[root@shell_example mnt]# cp /etc/passwd .
[root@shell_example mnt]# vim passwd 
[root@shell_example mnt]# cat passwd 

在这里插入图片描述

1.过滤文件中的root

[root@shell_example mnt]# grep root passwd

在这里插入图片描述

2.过滤文件中的root,root不区分大小写

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

在这里插入图片描述

3.过滤文件中root前面没有字符

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

在这里插入图片描述

4.过滤文件中root前面和后面都没有字符

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

在这里插入图片描述

5.过滤文件中root后面没有字符

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

在这里插入图片描述

6.过滤文件中以root开头且root前后都没有字符

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

在这里插入图片描述

7.过滤文件中以root结尾且root前后都没有字符

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

在这里插入图片描述

8.过滤文件中以root开头或以root结尾的字符

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

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

注意:egrep和grep -iE是一样的

9.过滤文件中root在中间的字符

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

在这里插入图片描述

10.过滤文件中root在中间且前后没有字符

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

在这里插入图片描述

  • grep中的正则表达式

1.过滤以r开头以t结尾的四个字符的内容

[root@shell_example mnt]# grep r..t passwd

在这里插入图片描述

2.过滤以r开头的五个字符的内容

[root@shell_example mnt]# grep r.... passwd

在这里插入图片描述

3.过滤以t结尾的四个字符的内容

[root@shell_example mnt]# grep ...t passwd

在这里插入图片描述

4.过滤以oo在中间的四个字符的内容

[root@shell_example mnt]# grep .oo. passwd

在这里插入图片描述

  • grep中字符匹配次数设定
 字符出现0-任意次
\? 字符出现0-1次
\+ 字符出现1-任意次
\{n\} 字符出现n次
\{m,n\} 字符出现m-n次
\{0,n\}	字符揣按0-n次
\{m,\} 字符出现至少m次
\{(xy\)\{n\}xy} xy关键字出现n次
.* 关键字之间匹配任意字符

实验前提:

[root@shell_example mnt]# vim test
[root@shell_example mnt]# cat test
xy
xxxxy
xyxyxy
xyyyyyyy
yyyyyy

在这里插入图片描述

1.test中字符x出现

[root@shell_example mnt]# grep x test

在这里插入图片描述

2.test的中字符x出现0-任意次

[root@shell_example mnt]# grep -E "x*" test 

在这里插入图片描述

3.test的中字符x出现0-1次

[root@shell_example mnt]# grep -E "x?" test 

在这里插入图片描述

4.test的中字符x出现1-任意次,y出现1-任意次

[root@shell_example mnt]# grep -E "x+y" test 

在这里插入图片描述

5.test的中字符x出现3次,y出现1次

[root@shell_example mnt]# grep -E "x{3}y" test

在这里插入图片描述

6.test的中字符x出现1-3次,y出现1次

[root@shell_example mnt]# grep -E "x{1,3}y" test 

在这里插入图片描述

7.test的中字符x出现0-3次,y出现1-任意次

[root@shell_example mnt]# grep -E "x{,3}y" test

在这里插入图片描述

8.test的中字符x出现1-任意次,y出现1-任意次

[root@shell_example mnt]# grep -E "x{1,}y" test

在这里插入图片描述

9.test的中字符xy出现0-1次

[root@shell_example mnt]# grep -E "(xy)?" test

在这里插入图片描述

10.test的中字符xy出现2到任意次

[root@shell_example mnt]# grep -E "(xy){2,}" test

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值