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