仅从文本文件中删除完全成形的行范围,同时忽略那些只有起始分隔符的行范围 [英] Delete only fully formed line ranges from a text file while ignoring those that only have a start delimiter

查看:12
本文介绍了仅从文本文件中删除完全成形的行范围,同时忽略那些只有起始分隔符的行范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除 START 和 END 关键字之间的行,如下所述:

I what to delete lines between START and END keywords as described below:

START
text1
text2
text3
START
text4
END
text5
text6
START
test7
START
test8
END

我的问题在于 START 关键字并不总是以 END 结束.如上例所示,第一个 START 没有以 END 结束,而是在 TEXT3 之后再次以另一个 START 结束.

My problem is in the START keyword not always closed with END. As from the example above the first START did not close with END but another START again after TEXT3.

所以我不能使用以下 sed 命令:

So I cannot use the following sed command:

sed '/START/,/END/d' test.txt

因为它会删除从 TEXT1 到 TEXT 4 以及 TEXT7-8 的行.

because it will delete the lines from TEXT1 to TEXT 4 and also TEXT7-8.

但我只想删除行 TEXT4 和 TEXT8.所以下面的输出应该是这样的:

But I want only to delete lines TEXT4 and TEXT8. So the following output should be like this:

START
text1
text2
text3
text5
text6
START
text7

推荐答案

通过按行反转文件变得更容易:

becomes easier by reversing the file linewise:

$ tac test.txt | sed '/END/,/START/d' | tac
START
text1
text2
text3
text5
text6
START
test7

这篇关于仅从文本文件中删除完全成形的行范围,同时忽略那些只有起始分隔符的行范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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