Sed - 打印从正则表达式到(正则表达式或eof)的行

时间:2013-03-03 11:01:17

标签: sed

如何使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

子问题:只打印第一个这样的事件。

1 个答案:

答案 0 :(得分:1)

匹配^ START到^ END

的所有行
 sed -n '/^START/,/^END/p' <file>

打印首次出现

 sed -n '/^START/,/^END/ {p;q;}' <file>