栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

linux三剑客之grep命令详解

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux三剑客之grep命令详解

linux三剑客之grep命令详解

grep是对文本进行过滤的工具,以行为单位,grep的主要作用就是与管道符和正则表达式结合使用,在linux三剑客的使用过程中,管道符和正则表达式特别重要。
之前文章说过,从管道符接受的数据为标准输入,所以grep支持从标准输入或者命令行获取参数,例如cat a.txt | grep 'hello'与 grep 'hello' a.txt都是可以的。

grep格式:
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.包含转义字符的正则和扩展正则
包含转义的正则表达式和扩展正则表达式

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/512614.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号