grep 精确匹配
Posted Howhy Blogs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了grep 精确匹配相关的知识,希望对你有一定的参考价值。
使用
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
以上是关于grep 精确匹配的主要内容,如果未能解决你的问题,请参考以下文章