linux 中正则匹配限制词首和词尾

发布时间 2023-12-24 11:42:27作者: 小鲨鱼2018

 

001、\<或者\b限制词首

[root@pc1 test]# ls
a.txt
[root@pc1 test]# cat a.txt                   ## 测试数据
01 02 03 04 05
06 07 08 09 10
11 12 13 14 15 13783120433
16 17 18 19 20
21 22 23 24 25 24332233443
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40
[root@pc1 test]# grep "\<2" a.txt            ## 限制词首
16 17 18 19 20
21 22 23 24 25 24332233443
26 27 28 29 30
[root@pc1 test]# grep "\b2" a.txt            ## 限定词首
16 17 18 19 20
21 22 23 24 25 24332233443
26 27 28 29 30

 

002、\>或者\b限制词尾

[root@pc1 test]# ls
a.txt
[root@pc1 test]# cat a.txt                         ## 测试数据
01 02 03 04 05
06 07 08 09 10
11 12 13 14 15 13783120433
16 17 18 19 20
21 22 23 24 25 24332233443
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40
[root@pc1 test]# grep "2\>" a.txt                 ## 限制词尾
01 02 03 04 05
11 12 13 14 15 13783120433
21 22 23 24 25 24332233443
31 32 33 34 35
[root@pc1 test]# grep "2\b" a.txt                 ## 限制词尾
01 02 03 04 05
11 12 13 14 15 13783120433
21 22 23 24 25 24332233443
31 32 33 34 35

 。