grep最后一场比赛,它是以下几行 [英] grep last match and it's following lines
问题描述
我已经学习了如何在比赛之前和之后grep
行以及如何在最后一场比赛grep
中找到我,但我还没有发现如何grep
最后一场比赛及其下一行.
I've learnt how to grep
lines before and after the match and to grep
the last match but I haven't discovered how to grep
the last match and the lines underneath it.
该方案是服务器日志.
我想列出命令的动态输出.一个服务器日志中可能多次使用该命令.因此,我认为匹配将是命令,并且grep
可以将-A
与其他标志或tail命令的变体一起使用,以完成我正在寻找的结果.
The scenario is a server log.
I want to list a dynamic output from a command. The command is likely to be used several times in one server log. So I imagine the match would be the command and somehow grep
can use -A
with some other flag or variation of a tail command, to complete the outcome I'm seeking.
推荐答案
我将采用这种方法来解决问题,因为它更容易找到第一个匹配项并打印上下文行.取得文件:
The approach I would take it to reverse the problem as it's easier to find the first match and print the context lines. Take the file:
$ cat file
foo
1
2
foo
3
4
foo
5
6
说我们要foo
的最后一个匹配项,然后是lines
的最后一个匹配项,我们可以使用tac
反转文件,使用-Bn
查找上面的第一个匹配项和n
行,然后停止使用
Say we want the last match of foo
and the following to lines
we could just reverse the file with tac
, find the first match and n
lines above using -Bn
and stop using -m1
. Then simple re-reverse the output with tac:
$ tac file | grep foo -B2 -m1 | tac
foo
5
6
tac
和rev
之类的工具可以使看起来很困难的问题变得容易得多.
Tools like tac
and rev
can make problems that seem difficult much easier.
这篇关于grep最后一场比赛,它是以下几行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!