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

 

posted @   小鲨鱼2018  阅读(665)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示