如何使sed
打印行从一个匹配的正则表达式(包括)到匹配的其他正则表达式(包括),但添加条件 - 结束正则表达式可能根本不存在,在这种情况下应该打印EOF的所有内容吗?
示例1(假设^START.*
和^END.*
作为分隔正则表达式):
cruft1
cruft2
START print this
print this
print this
END print this too
cruft
例2:
cruft1
cruft2
START print this
print this
print this
- file ends here
子问题:只打印第一个这样的事件。
答案 0 :(得分:1)
匹配^ START到^ END
的所有行 sed -n '/^START/,/^END/p' <file>
打印首次出现
sed -n '/^START/,/^END/ {p;q;}' <file>