1.grep all 过滤出含有all三个字母的所有语句 如:
all tooall
to alltoall all
allto100
uuualltoall
&&all
&all*
;all;
grep -w "all"过滤出来的是含有all这个单词的所有语句 也就是说只过滤字母数字,不过滤字符
all tooall
to alltoall all
&&all
&all*
;all;
![](https://i-blog.csdnimg.cn/blog_migrate/df150b1b1b914f6dac22415f8cf4839b.png)
2.grep --color=auto “root”1.txt 将root一红色标识出来
例子:cat 999.txt | grep qwer --color=auto
![](https://i-blog.csdnimg.cn/blog_migrate/1dcdeee2cf70d6535fa58fc81c7f841b.png)
3.grep -v 取反参数 也就是说过滤出除了 -v后面参数的所有数据
例子:
![](https://i-blog.csdnimg.cn/blog_migrate/0a6bb138920e5a2c49b2221482c1a2b0.png)
4.grep -c "root" 1.txt root这个文件在1.txt里出现的
行数
例子:
![](https://i-blog.csdnimg.cn/blog_migrate/6bf6fbb75d1410aade18707249afe354.png)
5.grep -n “qwer”999.txt 显示匹配的行数(在第几行出现的)
![](https://i-blog.csdnimg.cn/blog_migrate/cfa9b4a0b674684e131c6d46b0465929.png)
6.显示出匹配的文件 grep “qwer” 999.txt 888.txt 777.txt
![](https://i-blog.csdnimg.cn/blog_migrate/437f1b85ebcde6823a526d4a3027eb82.png)
7.忽略大小写字母 : grep -i “qwer” 999.txt
![](https://i-blog.csdnimg.cn/blog_migrate/36661b469bbe24761beedb6c53d7340b.png)
8.向上匹配,向下匹配,和向上向下分别匹配 (很常用!)
grep "root" -A 2 匹配含有root的那一行再多往下显示2行,最后显示三行
grep "root" -B 2 匹配含有root的那一行再多往上显示2行,最后显示三行
grep "root" -C 2 匹配含有root的那一行再多往上,往下显示2行,最后显示五行
![](https://i-blog.csdnimg.cn/blog_migrate/8309f8e42364bf423dfbfa9c7f8ab9dd.png)