多行,重复匹配 [英] Multiple line, repeated occurence matching

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

问题描述

我提到下面的问题,但有点不同。只有在efg匹配不同行时,才需要获取具有abc的行。我只需要在匹配efg前匹配最新的匹配abc行...

如何使用grep跨多行查找模式?

 等等等等.. 
等等等等..
等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等blah2
等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等abc blah3
blah blah ..
blah blah ..
blah abc blah4
blah blah ..
blah blah blah

blah abc blah5
等等等等..
等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等。 blah blah
blah abc blah7
blah blah ..
blah blah ..
blah abc blah8
blah blah ..

预计产量

  blah abc blah2 
blah abc blah6


这可能适用于你(GNU sed):

  sed -n' / abc / h; / efg /!b; x; / abc / p; z; x'文件



<将最新的 abc 行保存在保存空间(HS)中。当遇到包含 efg 的行时,切换到HS,并且如果该行包含 abc 打印它。


I refer to below question, but with a bit difference. I need to only get line that has "abc" when there is "efg" matching at different line. And I only need the latest matched "abc" line before "efg" is matched...

How to find patterns across multiple lines using grep?

blah blah..
blah blah..
blah abc blah1
blah blah..
blah blah..
blah abc blah2
blah blah..
blah efg1 blah blah
blah efg2 blah blah
blah blah..
blah blah..

blah abc blah3
blah blah..
blah blah..
blah abc blah4
blah blah..
blah blah blah

blah abc blah5
blah blah..
blah blah..
blah abc blah6
blah blah..
blah efg3 blah blah

blah efg4 blah blah
blah abc blah7
blah blah..
blah blah..
blah abc blah8
blah blah..

Expected output

blah abc blah2
blah abc blah6

解决方案

This might work for you (GNU sed):

sed -n '/abc/h;/efg/!b;x;/abc/p;z;x' file

Store the latest abc line in the hold space (HS). When encountering a line containing efg, switch to the HS and if that line contains abc print it.

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

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