linux下grep用法
[root@kylin10 ~]# nl test.txt
1 I love china
2 I love chinese
3 My lover is flower
1.grep -w
只匹配单词,第三行的lover就不会匹配了
[root@kylin10 ~]# nl test.txt
1 I love china
2 I love chinese
3 My lover is flower
################################
# grep -w love test.txt
I love china
I love chinese
2.grep -q
这个常用在shell中,静默输出,一般和qw进行搭配,效果更好
当我们过滤某一字符时,直接grep会显示很多,不友好,这时候用-q的时候会很直接
[root@kylin10 ~]# nl test.txt
1 I love china
2 I love chinese
3 My lover is flower
################################
[root@shell ~]# grep -q love test.txt
[root@shell ~]# echo $? #命令执行成功,返回0
0
[root@shell ~]# grep -q lovelove test.txt
[root@shell ~]# echo $? #命令执行失败,返回1
1
3.grep -A -B -C
-A 显示匹配到的字符串所在的行及其后n行 after
-B 显示匹配到的字符串所在的行及其前n行 before
-C 匹配前后各n行