10

input.txt:

blah1
blah2
start_pattern
blah3
blah4
blah5
end_pattern
blah6
blah7

I would like input.txt to become:

blah1
blah2
end_pattern
blah6
blah7

I tried the following, but it didn't work.

sed -i "/start_pattern/, /end_pattern/{$!d}" input.txt
4

2 回答 2

8

这个问题涉及您的问题:

$ sed '/start_pattern/,/end_pattern/{/end_pattern/!d}' foo.txt
blah1
blah2
end_pattern
blah6
blah7
于 2013-05-19T10:11:44.387 回答
6

sed 是在单行上进行简单替换的出色工具。对于任何其他文本操作,只需使用 awk 即可获得更清晰、更简单、更易于扩展的解决方案:

$ awk '/start_pattern/{f=1} /end_pattern/{f=0} !f;' file
blah1
blah2
end_pattern
blah6
blah7

也可以看看:

$ awk '/start_pattern/{f=1} !f; /end_pattern/{f=0}' file
blah1
blah2
blah6
blah7

$ awk '!f; /start_pattern/{f=1} /end_pattern/{f=0}' file
blah1
blah2
start_pattern
blah6
blah7

$ awk '/start_pattern/{f=1} /end_pattern/{f=0} f;' file
start_pattern
blah3
blah4
blah5

$ awk '/start_pattern/{f=1} f; /end_pattern/{f=0}' file
start_pattern
blah3
blah4
blah5
end_pattern

$ awk 'f; /start_pattern/{f=1} /end_pattern/{f=0}' file
blah3
blah4
blah5
end_pattern
于 2013-05-19T12:36:10.497 回答