案例1:精准匹配(-w)
[root@localhost test]# cat 1.txt | grep root 1.txt --color=auto
![](https://img.haomeiwen.com/i8166147/1f0ed57c4dc9c950.png)
[root@localhost test]# cat 1.txt | grep -w "root" 1.txt --color=auto
![](https://img.haomeiwen.com/i8166147/fa9197076da4f9a3.png)
注: 可以看出来,加
-w
参数会精准匹配要匹配的单词,并且是区分呢大小写匹配。其中 参数--color=auto
是加入自动颜色,就是我们匹配的单词高亮显示
案例2:取反参数(-v)
[root@localhost test]# ps -ef | grep ssh
![](https://img.haomeiwen.com/i8166147/b389bf11f239323e.png)
[root@localhost test]# ps -ef | grep ssh | grep -v grep
![](https://img.haomeiwen.com/i8166147/71b72131dec21afa.png)
案例3:统计出现的行数数量(-c)
[root@localhost test]# grep -c "root" 1.txt
![](https://img.haomeiwen.com/i8166147/81802d782030f838.png)
案例4:显示匹配的行数(-n)
[root@localhost test]# grep -n "root" 1.txt --color=auto
![](https://img.haomeiwen.com/i8166147/e9f68d9b06951acb.png)
案例5:显示匹配的文件(-l)
[root@localhost test]# grep "root" 1.txt 2.txt 3.txt
![](https://img.haomeiwen.com/i8166147/4312d4b6be4353ff.png)
[root@localhost test]# grep -l "root" 1.txt 2.txt 3.txt
![](https://img.haomeiwen.com/i8166147/2fe5085a2b6beee0.png)
案例6:忽略文件大小写(-i)
[root@localhost test]# cat 1.txt | grep -i "root" --color=auto
![](https://img.haomeiwen.com/i8166147/efaf8842341ffa48.png)
案例7:控制字符范围
[root@localhost test]# seq 10 | grep "5" -A 3
![](https://img.haomeiwen.com/i8166147/89d44422faa8f9ef.png)
[root@localhost test]# seq 10 | grep "5" -B 3
![](https://img.haomeiwen.com/i8166147/6979ea730805f415.png)
[root@localhost test]# seq 10 | grep "5" -C 3
![](https://img.haomeiwen.com/i8166147/648371fa10d38db4.png)
通过以上图示实验,可以明白:
-A n 向下匹配n行
-B n 向上匹配n行
-C n 同时向上向下匹配n行
网友评论