SED:返回最后一次出现的匹配,直到文件结束 [英] sed: return last occurrence match until end of file

查看:2156
本文介绍了SED:返回最后一次出现的匹配,直到文件结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SED,我怎么返回比赛的最后一次数,直到最后的文件?
(FYI这已被简化的)

到目前为止,我已经试过:

  SED -n'/统计| /,$ P'LOGFILE.LOG

从首场比赛将返回所有行起(几乎整个文件)

我也试过:

  $ LINENUM =`尾-400 LOGFILE.LOG | grep的-n统计| |尾-1 |切-d:-f1`
SED$ LINENUM,\\ $!DLOGFILE.LOG

这工作,但不会工作在一个命令ssh连接,确实需要这一切是在一个流水线。

日志文件的格式如下所示:

(没有与写入日志文件每分钟子数据统计报头,这个命令的目的是返回最近的统计与头之后发生的任何相关错误一起头)

 统计|
   东东
   更多的东西
   更东西
统计|
   东东
   更多的东西
错误:不正确的值
统计|
   东东
   更多的东西
   更东西
统计|
   东东
错误:错误类型有一个
错误:错误式两种

EOF

返回必须是:

 统计|
   东东
错误:错误类型有一个
错误:错误式两种


解决方案

如果您有 TAC 可用:

  TAC INPUTFILE | SED'/ ^统计| / Q'| TAC

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)

I've also tried:

$linenum=`tail -400 logfile.log | grep -n " Statistics |" | tail -1 | cut -d: -f1`
sed "$linenum,\$!d" logfile.log

This works but won't work over an ssh connection in one command, really need it all to be in one pipeline.

Format of the log file is as follows:

(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

Return needs to be:

Statistics |
   Stuff
Error: error type one
Error: error type two

解决方案

If you have tac available:

tac INPUTFILE | sed '/^Statistics |/q' | tac

这篇关于SED:返回最后一次出现的匹配,直到文件结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆