shell脚本学习之四剑客grep

简述
grep是一种强大的文本搜索工具,与正则表达式配合搜索文本,并把匹配的行打印出来。


语句格式:

grep -options ‘word’ filename

常用参数:
-a:以文本文件方式搜索
-c:计算找到的符合行的次数
-i:忽略大小写
-n:顺便输出行号
-v:反向选择,输出不包含匹配的所有行
-h:查询多文件时不现实文件名
-l:查询多文件时,只输出包含匹配字符的文件名
-s:不显示不存在或无匹配文本的错误信息
-E:使用egrep扩展匹配模式
– color:指定匹配输出字符的颜色
正则表达式
点这里
实例

[root@jiangpeisi shell]# cat a.txt | grep '^m.*l$' 
mysql

[root@jiangpeisi shell]# cat a.txt | grep -E "\<([0-9]{1,3}\.){3}([0-9]{1,3})\>" 
192.168.32.1

[root@jiangpeisi shell]# cat a.txt | grep -c  '^m.*l$' 
1

[root@jiangpeisi shell]# cat a.txt | grep  -n '^m.*l$' 
2:mysql

[root@jiangpeisi shell]# cat a.txt | grep  -i -n  '^M.*l$' 
2:mysql

[root@jiangpeisi shell]# netstat -ntlp | grep named
tcp        0      0 172.17.0.1:53           0.0.0.0:*               LISTEN      3585/named          
tcp        0      0 172.16.30.80:53         0.0.0.0:*               LISTEN      3585/named          
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN      3585/named          
tcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTEN      3585/named