【shell】grep:强大的文本搜索工具

一. 基本认识

grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配到的标红。
grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。

grep的工作方式:
它在一个或多个文件中逐行读取每个文件或输入流的内容,并对每一行应用模式匹配搜索。搜索的结果被送到标准输出,不影响原文件内容。

如果要匹配多个文件,文件名之间以空格分隔,如:grep "match_pattern" file_1 file_2 file_3 ...

 

命令参数:

 -A<显示行数>:除了显示符合范本样式的那一列之外,并显示该行之后的内容。
 -B<显示行数>:除了显示符合样式的那一行之外,并显示该行之前的内容。
 -C<显示行数>:除了显示符合样式的那一行之外,并显示该行之前后的内容。
 -c:统计匹配的行数
 -e :实现多个选项间的逻辑or 关系
 -E:扩展的正则表达式
 -f FILE:从FILE获取PATTERN匹配
 -F :相当于fgrep
 -i --ignore-case #忽略字符大小写的差别。
 -n:显示匹配的行号
 -o:仅显示匹配到的字符串
 -q: 静默模式,不输出任何信息
 -s:不显示错误信息。
 -v:显示不被pattern 匹配到的行,相当于[^] 反向匹配
 -w :匹配 整个单词

 

二. 基础例子

[root@localhost ~]# grep [-ivnc] '需要匹配的字符' 文件名
#-i不区分大小写
#-c统计包含匹配的行数
#-n输出行号
#-v反向匹配

1. 包含某个字符串

#查找包含name所在行
[root@localhost ~]# grep 'name' tomAndJerry.txt
The cat's name is Tom,what's the mouse's name?
# 忽略大小写
[root@localhost ~]# grep -i 'name' tomAndJerry.txt
The cat's name is Tom,what's the mouse's name?
The mouse's Name is Jerry

2. 查找字符串所在位置、出现行数

#打印出含有name的行所在位置
[root@localhost ~]# grep -n 'name' tomAndJerry.txt
1:The cat's name is Tom,what's the mouse's name?

# c:包含name的行数
[root@localhost ~]# grep -c 'name' tomAndJerry.txt
1
# ci:包含name(不区分大小写)的行数
[root@localhost ~]# grep -ci 'name' tomAndJerry.txt
2

3. 反向匹配

# 打印不包含name的行
[root@localhost ~]# grep -v 'name' tomAndJerry.txt
The mouse's Name is Jerry
They are good friends

# 打印不包含name(不区分大小写)的行
[root@localhost ~]# grep -vi 'name' tomAndJerry.txt
They are good friends

 
 

三. 正则例子

例子文本

[root@localhost ~]# cat RegExp.txt
----TEXT BEGIN----

good morning teacher
hello world is a script
gold sunshine looks beautiful
golden time flies
god bless me
what a delicious food
they teast Good
you fell glad
wrong word gooood
wrong word gl0d
wrong word gl2d
wrong word gl3d
www.helloworld.com
www@helloworld@com
Upper case
100% means pure
php have a gd module

-----TEXT END-----

1. 匹配行首、行尾

#搜索以good开头的行
[root@localhost ~]# grep '^good' RegExp.txt
good morning teacher

匹配行尾

#搜索以Good结尾的行
[root@localhost ~]# grep 'Good$' RegExp.txt
they teast Good

 

2. 计算空行数量

#搜索空行的行数
[root@localhost ~]# grep -c '^$' RegExp.txt
2

 

3. 匹配多种可能

#搜索包含Good和good的行
[root@localhost ~]# grep '[Gg]ood' RegExp.txt
good morning teacher
they teast Good

 

4. 反选搜索

#搜索一个包含ood的行,但是不能是Good或good
#尖角号表示的是“非”

[root@localhost ~]# grep '[^Gg]ood' RegExp.txt
what a delicious food
wrong word gooood

 

5. 匹配固定数量的任意字符

#搜索包含一个词,该词以g开头、紧接着是两个任意字符、再接着是一个d的行
[root@localhost ~]# grep 'g..d' RegExp.txt
good morning teacher
gold sunshine looks beautiful
golden time flies
you fell glad
wrong word gl0d
wrong word gl2d
wrong word gl3d

#搜索包含一个词,该词以G或g开头、紧接着是两个任意字符、再接着是一个d的行
[root@localhost ~]# grep '[Gg]..d' RegExp.txt
good morning teacher
gold sunshine looks beautiful
golden time flies
they teast Good
you fell glad
wrong word gl0d
wrong word gl2d
wrong word gl3d

#搜索这样一些行,该行包含某个单词,该词满足如下条件:
#1.第一个字符可以是G或g
#2.第二个字符可以是l或o
#3.第三个字符可以是换行符之外的任意字符
#4.第四个字符一定是d
[root@localhost ~]# grep '[Gg][lo].d' RegExp.txt
good morning teacher
gold sunshine looks beautiful
golden time flies
they teast Good
you fell glad
wrong word gl0d
wrong word gl2d
wrong word gl3d

 

6. 匹配零到无限个固定字符

#搜索这样一些行,该行包含某个单词,该词满足如下条件:
#1.以g开头
#2.g后面跟零到无限个o
#3.零到无限个o后面跟d
[root@localhost ~]# grep go*d RegExp.txt
good morning teacher
god bless me
wrong word gooood
php have a gd module

 

7. 使用扩展的正则表达式egrep

#搜索g和d之间至少有一个o的行
#“+”代表匹配前面的字符1次以上(含1次)
[root@localhost ~]# egrep 'go+d' RegExp.txt
good morning teacher
god bless me
wrong word gooood

#搜索g和d之间只有0个或1个o的行(0次或1次)
#“?”代表匹配前面的字符1次以上
[root@localhost ~]# egrep 'go?d' RegExp.txt
god bless me
php have a gd module

#搜索有glad或gold的行
[root@localhost ~]# egrep 'glad|gold' RegExp.txt
gold sunshine looks beautiful
golden time flies
you fell glad

#搜索有glad或gold的行的另一种写法
[root@localhost ~]# egrep 'g(la|ol)d' RegExp.txt
gold sunshine looks beautiful
golden time flies
you fell glad

 
 
参考:
《linux系统命令及Shell脚本实践指南》

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

roman_日积跬步-终至千里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值