Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

linux - Delete only fully formed line ranges from a text file while ignoring those that only have a start delimiter

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

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.

So I cannot use the following sed command:

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

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

becomes easier by reversing the file linewise:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...