匹配模式跨越多行并删除那些匹配的行 [英] Matching pattern span multiple line and remove those matching lines

查看:55
本文介绍了匹配模式跨越多行并删除那些匹配的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 sed 执行以下操作:

I was trying to use sed to do the following:

在文件中看起来像:

 FirstLine
 SecondLineEEE
 AAAblablabla
 ForthLineEEE
 FifthLine
 LastLine

我想删除 EEE(但保留第二行的其余部分)和以 AAA 开头的整行,并保持文件的其他部分完整.

I want to remove EEE (but keep the rest of second line) and the whole line starting with AAA and keep other part of the file intact.

预期的结果是(如所见,如果下一行不以AAA开头,则会保留,这就是为什么我需要匹配多行.)

The expected result is (as seen, if the following line doesn't start with AAA, it will be kept, that's why I need to match multiple lines.)

 FirstLine
 SecondLine
 ForthLineEEE
 FifthLine
 LastLine

我该怎么做?提前致谢!

How should I do it? Thanks in advance!

推荐答案

这可能对你有用:

echo -e "FirstLine\nSecondLineEEE\nAAAblablabla\nLastLine" |
sed '/EEE$/{N;s/EEE\nAAA.*//}'
FirstLine
SecondLine
LastLine

这篇关于匹配模式跨越多行并删除那些匹配的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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