使用
grep
实现精确过滤的五种方法
(1)当被过滤的内容占据一行时
[root@MySQL scripts]
# cat oldboy.log
200
0200
2000
[root@MySQL scripts]
# grep "200" oldboy.log
200
#<==正则按单词匹配
[root@MySQL scripts]
# grep -w "200" oldboy.log
200
#<==按单词匹配
[root@MySQL scripts]
# grep -x "200" oldboy.log
200
#<==按整行匹配
[root@MySQL scripts]
# grep "^200$" oldboy.log
200
#<==正则开头结尾匹配,相当于整行匹配
[root@MySQL scripts]
# grep -E "<200>" oldboy.log
200
#<==扩展正则
(2)当被过滤的内容占据一行的一部分
[root@MySQL scripts]
# cat oldboy.log
200 oldboy
0200 oldgirl
2000 littleboy
[root@MySQL scripts]
# grep "200" oldboy.log
200 oldboy
[root@MySQL scripts]
# grep -w "200" oldboy.log
200 oldboy
[root@MySQL scripts]
# grep -x "200" oldboy.log
#<==按整行匹配,所以没结果
[root@MySQL scripts]
# grep -x "200 oldboy" oldboy.log
200 oldboy
[root@MySQL scripts]
# grep "^200$" oldboy.log
#<==按整行匹配,所以没结果
[root@MySQL scripts]
# grep "^200 oldboy$" oldboy.log
200 oldboy
[root@MySQL scripts]
# grep -E "<200>" oldboy.log
200 oldboy