sed多行模式缓冲区在最后一行刷新 - 正常行为?

时间:2015-09-09 20:12:05

标签: regex sed

考虑这个输入文件。

# cat test.txt
some other stuff
some more other stuff
first line
second thingy
third whatsit
fourth oojiflop
fifth item
sixth widget
this is the
end of file

运行这个衬里就像我想要的那样(我在OSX上创建了多个sed命令)。它会删除“第一行”和“第五项”之间的所有行,但仅限于出现“第五项”。

sed -e '/^first line/{' -e ':no' -e '$!{N' -e '/fifth item$/ b y' -e 'b no' -e ':y' -e 'd;};}' test.txt

我担心如果“第五项”模式在“第一行”之后的行中不匹配,那么它将不会打印“第一行”之后的任何行。但确实如此。它似乎适用于GNU风格以及BSD / OSX。

所以 - 如果我跑

sed -e '/^first line/{' -e ':no' -e '$!{N' -e '/xxxxxxx$/ b y' -e 'b no' -e ':y' -e 'd;};}' test.txt

然后,当它到达^第一行时,它将开始使用'N'命令填充模式缓冲区。它将继续,寻找xxxxxxx,直到它击中EOF,和$!测试失败。此时,执行从外部大括号返回,在终止之前,sed似乎将整个模式缓冲区转储到std out。

我没有意识到这种行为 - 我的问题是这是否是正常行为以及记录的位置(当$!测试失败时整个模式缓冲区被转储的事实)

0 个答案:

没有答案