Sed - 从正则表达式到(正则表达式或eof)
问题描述:
如何使sed
从一个匹配的正则表达式(包含)到另一个匹配的正则表达式(包括)的打印行,但是添加了条件 - 关闭正则表达式可能不会是否存在,在这种情况下,直到EOF的所有内容都应该打印出来?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
Subquestion:打印仅第一个这样的occurence。
答
匹配的所有行从^开始到^ END
sed -n '/^START/,/^END/p' <file>
打印一次出现
sed -n '/^START/,/^END/ {p;q;}' <file>
有趣,对我的作品 - 庆典上MAC OS V6.8 – suspectus 2013-03-03 11:19:56
我会重新检查。 – 2013-03-03 11:20:56
第一个也适用于我,直到EOF,但不是第二个。它会在START处执行'q'。您将需要第二个条件,如:'sed -n'/^START /,/^END/{p;/^ END/q;}'' –
Birei
2013-03-03 11:25:26