[英]In Linux: Print last line of log file that starts with
I am trying to have the output of a single OS command display the last line of a log file, that starts with a specific string. 我试图使单个OS命令的输出显示日志文件的最后一行,该日志文件以特定字符串开头。
So basically I have a log file that reads: 所以基本上我有一个日志文件,内容为:
Tom
Paul
Tom
Steve
Anthony
Tom
I want to type a command similar to this: 我想输入与此类似的命令:
tail -1 /etc/logfile | grep Steve
and have output like this: Steve 并输出如下:Steve
Unfortunately, the output of tail -1
only shows the last line of the log file...IF it matches. 不幸的是, tail -1
的输出仅显示日志文件的最后一行...如果匹配。 I want it to search the log file, from the bottom to the top (most recent to oldest) and print the first entry that matches the "Starts with" or grep. 我希望它从底部到顶部(最新到最旧)搜索日志文件,并打印与“开始于”或grep相匹配的第一项。 Thank you. 谢谢。 I tried a bunch of combinations of tail, sed, cat and less...but I may be just doing something small wrong and missing it. 我尝试了一些尾巴,sed,猫和更少的组合...但是我可能只是在做一些小错误而错过了它。
If the log is small enough that you don't care to scan it entirely, then 如果日志足够小,以至您无需扫描整个日志,那么
grep Steve etc/logfile | tail -n 1
will do your job. 会做你的工作。 If the file is really big, you don't want to grep it since it will impact performance, so I will suggest building a script that read the file in reverse and stops in the first occurrence. 如果该文件确实很大,则不希望对其重复处理,因为它会影响性能,因此,我建议您构建一个脚本,以反向读取文件并在第一次出现时停止。
Just do it the other way round grep Steve /etc/logfile | tail -l
用grep Steve /etc/logfile | tail -l
的另一种方式来做grep Steve /etc/logfile | tail -l
grep Steve /etc/logfile | tail -l
grep Steve /etc/logfile
Gets all the lines with Steve then 然后获取与史蒂夫的所有台词
tail -1
Prints the last of those. 打印其中的最后一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.