grep最后一场比赛,它是以下几行 [英] grep last match and it's following lines

查看:117
本文介绍了grep最后一场比赛,它是以下几行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学习了如何在比赛之前和之后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行,然后停止使用.然后使用tac简单地反转输出:

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

tacrev之类的工具可以使看起来很困难的问题变得容易得多.

Tools like tac and rev can make problems that seem difficult much easier.

这篇关于grep最后一场比赛,它是以下几行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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