sed:返回最后出现的匹配,直到文件结束 [英] sed: return last occurrence match until end of file
问题描述
使用 sed,我如何返回匹配的最后一次出现直到文件结束?(仅供参考,这已被简化)
Using sed, how do I return the last occurance of a match until the End Of File? (FYI this has been simplified)
到目前为止我已经尝试过:
So far I've tried:
sed -n '/ Statistics |/,$p' logfile.log
从第一个匹配项开始返回所有行(几乎整个文件)
Which returns all lines from the first match onwards (almost the entire file)
我也试过:
$linenum=`tail -400 logfile.log | grep -n " Statistics |" | tail -1 | cut -d: -f1`
sed "$linenum,$!d" logfile.log
这行得通,但不能在一个命令中通过 ssh 连接工作,真的需要将所有内容都集中在一个管道中.
This works but won't work over an ssh connection in one command, really need it all to be in one pipeline.
日志文件格式如下:
(每分钟都会有统计头和子数据写入日志文件,此命令的目的是返回最新的统计头以及头后发生的任何相关错误)
(There are statistics headers with sub data written to the log file every minute, the purpose of this command is to return the most recent Statistics header together with any associated errors that occur after the header)
Statistics |
Stuff
More Stuff
Even more Stuff
Statistics |
Stuff
More Stuff
Error: incorrect value
Statistics |
Stuff
More Stuff
Even more Stuff
Statistics |
Stuff
Error: error type one
Error: error type two
EOF
返回需要:
Statistics |
Stuff
Error: error type one
Error: error type two
推荐答案
如果您有 tac
可用:
tac INPUTFILE | sed '/^Statistics |/q' | tac
这篇关于sed:返回最后出现的匹配,直到文件结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!