-o 精确匹配
-w 显示匹配行
-i 忽略大小写
-n 显示行号
-v 反向匹配
-r 递归查找-E 正则匹配
-A 匹配往下行
-B 匹配往上行
-C 匹配往上往下行
^ 匹配开头行
$ 匹配结尾行
eg:
不区分大小写匹配ssh
grep -iw ssh /etc/services
精确匹配小写ssh
grep -o ssh /etc/services
不区分大小写匹配非ssh的行
grep -iv ssh /etc/services
显示行号精确匹配大写ssh
grep -no SSH /etc/services
递归查找
grep -r sshd /etc/
匹配根 (\) 结尾,向下1行
df -hT | grep '\/$' -A 1
匹配根 (\) 结尾,向上1行
df -hT | grep '\/$' -B 1
匹配根 (\) 结尾,上下各1行
df -hT | grep '\/$' -C 1
eg:
查找2个文件有相同/不同字段的行
[root@wai ~]# seq 1 3 > 1
[root@wai ~]# seq 1 2 > 2[root@wai ~]# cat 1
1
2
3[root@wai ~]# cat 2
1
2
[root@wai ~]# grep -wf 2 1
1
2
[root@wai ~]# grep -vwf 2 1
3