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

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

被折叠的 条评论
为什么被折叠?



