linux中grep命令锚定锚定单词的词首、词尾
1、测试数据
[root@centos79 test]# cat a.txt
a g e
u c j alike
i x k like
w f g liker
s g e g
[root@centos79 test]# grep like a.txt
u c j alike
i x k like
w f g liker
2、\b、\B锚定单词的词首
[root@centos79 test]# cat a.txt a g e u c j alike i x k like w f g liker s g e g [root@centos79 test]# grep "like" a.txt u c j alike i x k like w f g liker [root@centos79 test]# grep "\blike" a.txt i x k like w f g liker [root@centos79 test]# grep "\Blike" a.txt u c j alike
3、\b、\B锚定词尾
[root@centos79 test]# cat a.txt a g e u c j alike i x k like w f g liker s g e g [root@centos79 test]# grep "like" a.txt u c j alike i x k like w f g liker [root@centos79 test]# grep "like\b" a.txt u c j alike i x k like [root@centos79 test]# grep "like\B" a.txt w f g liker
4、同时锚定词首和词尾
[root@centos79 test]# cat a.txt a g e u c j alike i x k like w f g liker s g e g [root@centos79 test]# grep "like" a.txt u c j alike i x k like w f g liker [root@centos79 test]# grep "\blike\b" a.txt i x k like [root@centos79 test]# grep -w "like" a.txt i x k like
5、
[root@centos79 test]# cat a.txt a g e u c j alike i x k like w f g liker s g e g [root@centos79 test]# grep "\<like" a.txt i x k like w f g liker [root@centos79 test]# grep "like\>" a.txt u c j alike i x k like [root@centos79 test]# grep "\<like\>" a.txt i x k like
参考:https://www.cnblogs.com/flyor/p/6411140.html