正则表达式 – Awk或Sed:返回相同模式的两个实例之间的行
发布时间:2020-12-13 21:53:09 所属栏目:百科 来源:网络整理
导读:我有一个看起来像这样的文本文件: -+- beginning texthey therehi thereho there-+- ending textother stuffthat isimmaterial formy purposes 我只想抓住 – – 模式之间的界限,所以它会返回: hey therehi thereho there 标准awk方式: awk '/beginning t
我有一个看起来像这样的文本文件:
-+- beginning text hey there hi there ho there -+- ending text other stuff that is immaterial for my purposes 我只想抓住 – – 模式之间的界限,所以它会返回: hey there hi there ho there 标准awk方式: awk '/beginning text/ {flag=1;next} /ending text/ {flag=0} flag {print}' 只要“开始文本”和“结束文本”是不同的模式,效果很好. 唉,对于我的需要,“开始文本”和“结束文本”可以改变.这两行中唯一一致的部分是“ – – ”.文件中的所有其余文本可以完全不同;我不能依赖任何一致的模式.唯一可靠的文字是 – – .当两个字符串相同时,awk失败. 关于如何返回相同模式的两个离散实例之间的线条的任何想法,不包括包含模式的线条?不必是awk,只需要在bash shell脚本中工作.
如果图案相同并且您不希望打印出图案线,则只需在每次看到图案时通过反转标记来组合这两个图案.
awk '/^-+-/ {flag=!flag; next} flag {print}' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |