针对重叠的模式匹配进行缩放 [英] Grepping for overlapping pattern matches

查看:81
本文介绍了针对重叠的模式匹配进行缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在运行的

pre $ grep -o',[tcb],'<<< r,t,c,q,cb,b

输出是

 ,t,
,b,

但我想得到

 ,t,
,c,
,b,

(我不希望没有前面的b,或者没有a的c因此,应该在'r',t,'c,qb,b,''r,t'中找到[tcb] ,c,qb,b和r,t,c,qb,b,'

但是看来,第一个模式匹配,然后grep不会在模式匹配的第二个实例中寻找这个



有没有办法解决这个问题,或者grep不是为了做到这一点

解决方案您可以使用 awk 来代替 grep code>,用记号分隔符作为逗号:

$ $ $ $ $ $ $ $ awk -v RS ='/ ^ [tcb] $ / {print RS $ 0 RS}'<<< r,t,c,q,cb,b,

,t,
,c,
,b,


This is what I'm running

grep -o ',[tcb],' <<< "r,t,c,q,c b,b,"

The output is

,t,
,b,

But I want to get

,t,
,c,
,b,

(I do not want the b without a preceding , or the c without a trailing , to be matched)

Because ,[tcb], should be found in 'r",t,"c,q b,b,' 'r,t",c,"q b,b,' and 'r,t,c,q b",b,"'

But it seems that when the , is included in the first pattern match then grep does not look for this in the second instance of the pattern match

Is there a way around this or is grep not meant to do this

解决方案

You can use awk instead of grep for this with record separator as comma:

awk -v RS=, '/^[tcb]$/{print RS $0 RS}' <<< "r,t,c,q,c b,b,"

,t,
,c,
,b,

这篇关于针对重叠的模式匹配进行缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆