shell 中的文本处理工具(一)grep 文本过滤命令

本文详细介绍了Linux shell中的grep命令,作为强大的文本搜索工具,grep支持正则表达式,可用于查找和过滤文件中匹配特定模式的行。内容涵盖了grep的基本用法、正则表达式的应用,以及不同匹配条件的设置,通过实例演示了grep在文本处理中的实用技巧。

1.grep: 文本过滤命令
Global search regular expression and print out the
line
全面搜索研究正则表达式并显示出来

2.grep 命令是一种强大的文本搜索工具 , 根据用户指定的“模
式”对目标文本进行匹配检查 , 打印匹配到的行
由正则表达式或者字符及基本文本字符所编写的过滤条件

3.grep 的格式

grep 匹配条件 处理文件作用
grep root passwd在passwd文件中找到包含root字符串的行
grep ^root passwd在passwd文件中找到以root开头的行
grep root$ passwd在passwd文件中找到以root结尾的行
grep -i root passwd忽略大小写 -i
grep -E “root|ROOT” passwd过滤包含root或者ROOT的行(管道符是扩展正则表达式,过滤符是过滤多个条件)

准备工作:

cp /etc/passwd  /mnt    ##将/etc/passwd文件复制到/mnt下

实验1:
(1)编辑复制过来的文件,内容如下

[root@shell_example mnt]# vim passwd
[root@shell_example mnt]# cat passwd

在这里插入图片描述
(2)过滤所有包括root字符串的行

[root@shell_example mnt]# grep root passwd
root:x:0:0:root:/root:/bin/bash
test:root:test
test:test:root
test:testroot:test
test:roottest:test

在这里插入图片描述
(3)忽略大小写过滤

[root@shell_example mnt]# grep  -i root passwd
root:x:0:0:root:/root:/bin/bash
test:ROOT:test
test:root:test
test:test:root
test:testroot:test
test:roottest:test

在这里插入图片描述
(4)忽略大小写过滤以root开头的行

[root@shell_example mnt]# grep  -i ^root passwd
root:x:0:0:root:/root:/bin/bash

在这里插入图片描述
(5)忽略大小写过滤以root结尾的行

[root@shell_example mnt]# grep  -i root$ passwd
test:test:root

在这里插入图片描述
(6)过滤包含root或者ROOT的行(过滤多个条件)

[root@shell_example mnt]# grep  -E "root|ROOT" passwd
root:x:0:0:root:/root:/bin/bash
test:ROOT:test
test:root:test
test:test:root
test:testroot:test
test:roottest:test

在这里插入图片描述
注意:如果不加双引号会出现以下结果:

[root@shell_example mnt]# grep  root|ROOT passwd
bash: ROOT: command not found...
^C^C

[1]+  Stopped                 grep --color=auto root | ROOT passwd

在这里插入图片描述
加上双引号不加-E会出现以下结果:

[root@shell_example mnt]# grep  "root|ROOT"  passwd

在这里插入图片描述
(7)egrep和grep -E 效果一样

[root@shell_example mnt]# egrep  "root|ROOT"  passwd
root:x:0:0:root:/root:/bin/bash
test:ROOT:test
test:root:test
test:test:root
test:testroot:test
test:roottest:test

在这里插入图片描述
4.grep 中的正则表达式

字符前后的.是占位符
(1)过滤以r开头的四个字符的内容

[root@shell_example mnt]# grep r... passwd
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
test:root:test
test:test:root
test:testroot:test
test:roottest:test

在这里插入图片描述
(2)过滤以r开头以t结尾的四个字符的内容

[root@shell_example mnt]# grep r..t passwd
root:x:0:0:root:/root:/bin/bash
test:root:test
test:test:root
test:testroot:test
test:roottest:test

在这里插入图片描述
(3)过滤以t结尾的四个字符的内容

[root@shell_example mnt]# grep ...t passwd
root:x:0:0:root:/root:/bin/bash
halt:x:7:0:halt:/sbin:/sbin/halt
ntp:x:38:38::/etc/ntp:/sbin/nologin
test:ROOT:test
test:root:test
test:test:root
test:testroot:test
test:roottest:test

在这里插入图片描述
(4)过滤中间两个字符是oo的四个字符的内容

[root@shell_example mnt]# grep .oo. passwd
root:x:0:0:root:/root:/bin/bash
test:root:test
test:test:root
test:testroot:test
test:roottest:test

在这里插入图片描述
5.grep 正则表达式与扩展正则表达式
正规的 grep 不支持扩展的正则表达式 ,竖线是用于表示”
或”的扩展正则表达式元字符 , 正规的 grep 无法识别
加上反斜杠 , 这个字符就被翻译成扩展正则表达式 , 就像 egrep
和grep -E 一样

6.grep 中字符的匹配位置设定

^ 关键字^ 以字符串开头
关键字 $$ 以字符串结尾
< 关键字< 字符串前面没有
关键字 >>\字符串后面没有
< 关键字 >字符串前后都没有别的字符

| 或 &与
|不属于标注正则表达式,属于扩展正则表达式
egrep 和 grep -E效果一样
-v 不符合条件的一切,反向

实验2:
(1)root字符串前面没有字符

[root@shell_example mnt]# grep -i "\<root" passwd
root:x:0:0:root:/root:/bin/bash
test:ROOT:test
test:root:test
test:test:root
test:roottest:test

在这里插入图片描述
(2)root字符串前面和后面都没有字符

[root@shell_example mnt]# grep -i "\<root\>" passwd
root:x:0:0:root:/root:/bin/bash
test:ROOT:test
test:root:test
test:test:root

在这里插入图片描述
(3)以root开头并且后面没有root后没有字符的

[root@shell_example mnt]# grep -i "^root\>" passwd
root:x:0:0:root:/root:/bin/bash

在这里插入图片描述
(4)以root结尾并且前面没有的

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

在这里插入图片描述
(5)以root开头并且后面没有的或者以root结尾前面没有
不加e或者-E不会出现结果

[root@shell_example mnt]# grep -i  "^root\>|\<root$" passwd
[root@shell_example mnt]# egrep -i  "^root\>|\<root$" passwd
root:x:0:0:root:/root:/bin/bash
test:test:root
[root@shell_example mnt]# grep -iE "^root\>|\<root$" passwd
root:x:0:0:root:/root:/bin/bash
test:test:root

在这里插入图片描述
(6)不以root开头并且后面没有的或者以root结尾前面没有

[root@shell_example mnt]# grep -iE "^root\>|\<root$" passwd -v
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
halt:x:7:0:halt:/sbin:/sbin/halt
ntp:x:38:38::/etc/ntp:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
test:ROOT:test
test:root:test
test:testroot:test
test:roottest:test

在这里插入图片描述
(7)不满足以root开头并且后面没有的或者以root结尾前面没有的所有中包含root的

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

在这里插入图片描述
(8)在不满足以root开头并且后面没有的或者以root结尾前面没有的条件中root前面没有后边也没有字符的

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

在这里插入图片描述
(9)passwd文件中以root开头后边没有的并且以root结尾前边没有的

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

在这里插入图片描述

7.grep 中字符的匹配次数设定

*字符出现 [0- 任意次 ]
?字符出现 [0-1 次 ]
+字符出现 [1- 任意次 ]
{n}字符出现 [n 次 ]
{m,n}字符出现 [ 最少出现 m 次,最多出现 n 次 ]
{0,n}字符出现 [0-n 次 ]
{m,}字符出现 [ 至少 m 次 ]
(xy){n}xy关键字出现 [n 次 ]
.*关键字之间匹配任意字符

注:在匹配次数时字符串没有指定出现多少次默认是1-任意次
效果和字符串+一样

准备工作

[root@shell_example mnt]# vim test
[root@shell_example mnt]# cat test
xy
xxxxy
xyxyxy
xyyyyyy
yyyyyyyy
xxxxxxxxx

在这里插入图片描述
实验3:
(1)test文件中的出现的x

[root@shell_example mnt]# grep x test
xy
xxxxy
xyxyxy
xyyyyyy
xxxxxxxxx
[root@shell_example mnt]# grep -E "x+" test
xy
xxxxy
xyxyxy
xyyyyyy
xxxxxxxxx

在这里插入图片描述
在这里插入图片描述
(2)test文件中字符x出现任意次

[root@shell_example mnt]# grep -E "x*" test
xy
xxxxy
xyxyxy
xyyyyyy
yyyyyyyy
xxxxxxxxx

在这里插入图片描述
(3)test文件中字符x出现0-1次

[root@shell_example mnt]# grep -E "x?" test
xy
xxxxy
xyxyxy
xyyyyyy
yyyyyyyy
xxxxxxxxx

在这里插入图片描述
(4)test文件中字符x出现x出现0-1次,y出现1-任意次

[root@shell_example mnt]# grep -E "x?y" test
xy
xxxxy
xyxyxy
xyyyyyy
yyyyyyyy

在这里插入图片描述
(5)test文件中字符x出现1-任意次y出现1-任意次

[root@shell_example mnt]# grep -E "x+y" test
xy
xxxxy
xyxyxy
xyyyyyy

在这里插入图片描述
(6)test文件中字符x出现3次并且y出现1-任意次

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

在这里插入图片描述
(7)test文件中字符x出现1-3次并且y出现任意次

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

在这里插入图片描述
(8)test文件中字符x出现0-3次并且y1-任意次

[root@shell_example mnt]# grep -E "x{,3}y" test
xy
xxxxy
xyxyxy
xyyyyyy
yyyyyyyy

在这里插入图片描述
(9)test文件中字符x出现1-任意次并且y出现1-任意次

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

在这里插入图片描述
(10)test文件中字符x出现1-任意次,y出现0-1次

[root@shell_example mnt]# grep -E "xy?" test
xy
xxxxy
xyxyxy
xyyyyyy
xxxxxxxxx

在这里插入图片描述
(11)test文件中字符xy出现2-任意次

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

在这里插入图片描述
(12)test文件中字符xy出现0-1次

[root@shell_example mnt]# grep -E "(xy)?" test
xy
xxxxy
xyxyxy
xyyyyyy
yyyyyyyy
xxxxxxxxx

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值