grep是对文本进行过滤的工具,以行为单位,grep的主要作用就是与管道符和正则表达式结合使用,在linux三剑客的使用过程中,管道符和正则表达式特别重要。
之前文章说过,从管道符接受的数据为标准输入,所以grep支持从标准输入或者命令行获取参数,例如cat a.txt | grep 'hello'与 grep 'hello' a.txt都是可以的。
grep 功能参数 “正则表达式” 文件 或 …| grep 功能参数 “正则表达式”grep常用参数:
| 功能参数 | 解释 |
|---|---|
| -c | 输出匹配到的行数,功能等同于wc -l |
| -i | 正则表达式忽略大小写 |
| -l | 查询多文件时只输出包含匹配字符的文件名 |
| -h | 查询多文件时不显示文件名 |
| -n | 显示匹配行的行号 |
| -s | 不显示文件不存在或无匹配文本的错误信息 |
| -v | 显示不包含匹配文本的行 |
| -A | 匹配行的后几行 |
| -B | 匹配行的前几行 |
| -E | 扩展正则表达式,等同于egrep |
测试文件
[root@linuxforliuhj test]# cat -n song.txt
1 You make me feel so happy.
2 Whenever Im with you.
3 You make me feel so special.
4 This love is too good to be true.
5 Rosemary Anne Nash
6 If you were a teardrop,In my eye,For fear of losing you,I would never cry。
7 And if the golden sun,Should cease to shine its light.
8 Just one smile from you,Would make my whole world bright.
9 Hannah Jo Kee
10 Since the first time I saw you,I felt something inside,
11 I dont know if it's love at first sight,I do know I really like you a lot。
12 Tanya C Medeiros
1.统计grep命令过滤出的行数,使用-c参数
[root@linuxforliuhj test]# cat song.txt | grep -c 'ld' 3
grep -c等同于wc -l
[root@linuxforliuhj test]# cat song.txt | grep 'ld' | wc -l 3
2.使用正则表达式时忽略字母大小写,使用-i参数
[root@linuxforliuhj test]# cat song.txt | grep '^if' [root@linuxforliuhj test]# cat song.txt | grep -i '^if' If you were a teardrop,In my eye,For fear of losing you,I would never cry。
3.过滤多文件时只显示包含匹配行的文件的文件名,使用-l参数
当使用命令cat song.txt song1.txt | grep -i 'tagya'时无法显示文件名,所以使用-l参数时无效
[root@linuxforliuhj test]# cp song.txt song1.txt [root@linuxforliuhj test]# grep -i 'tanya' song.txt song1.txt song.txt:Tanya C Medeiros song1.txt:Tanya C Medeiros [root@linuxforliuhj test]# grep -il 'tanya' song.txt song1.txt song.txt song1.txt [root@linuxforliuhj test]#
4.查询多文件时隐藏文件名,使用-h参数
[root@linuxforliuhj test]# grep -i 'tanya' song.txt song1.txt song.txt:Tanya C Medeiros song1.txt:Tanya C Medeiros [root@linuxforliuhj test]# grep -ih 'tanya' song.txt song1.txt Tanya C Medeiros Tanya C Medeiros
5.显示匹配的行号,使用-n参数
[root@linuxforliuhj test]# grep -in 'tanya' song.txt song1.txt song.txt:12:Tanya C Medeiros song1.txt:12:Tanya C Medeiros [root@linuxforliuhj test]# grep -ihn 'tanya' song.txt song1.txt 12:Tanya C Medeiros 12:Tanya C Medeiros
6.忽略文件不存在的报错,使用-s参数
当过滤的文件不存在时,会报错文件不存在,想要忽略此报错,需要使用-s参数
[root@linuxforliuhj test]# grep -i 'tanya' hello.sh grep: hello.sh: No such file or directory [root@linuxforliuhj test]# grep -i -s 'tanya' hello.sh [root@linuxforliuhj test]#
7.显示不包含匹配出的行,使用-v参数
[root@linuxforliuhj test]# grep -in 'i' song.txt 2:Whenever Im with you. 3:You make me feel so special. 4:This love is too good to be true. 6:If you were a teardrop,In my eye,For fear of losing you,I would never cry。 7:And if the golden sun,Should cease to shine its light. 8:Just one smile from you,Would make my whole world bright. 10:Since the first time I saw you,I felt something inside, 11:I dont know if its love at first sight,I do know I really like you a lot。 12:Tanya C Medeiros [root@linuxforliuhj test]# grep -inv 'i' song.txt 1:You make me feel so happy. 5:Rosemary Anne Nash 9:Hannah Jo Kee [root@linuxforliuhj test]#
8.过滤匹配行的前几行或者后几行,使用-A或者-B参数
例如过滤以‘If’开头的行以及该行的前3行和后2行
[root@linuxforliuhj test]# grep -A 2 -B 3 '^If' song.txt You make me feel so special. This love is too good to be true. Rosemary Anne Nash If you were a teardrop,In my eye,For fear of losing you,I would never cry。 And if the golden sun,Should cease to shine its light. Just one smile from you,Would make my whole world bright. [root@linuxforliuhj test]#
9.包含转义字符的正则和扩展正则
包含转义的正则表达式和扩展正则表达式



