我怎样才能让'grep'在grepped行之上显示一行五行? [英] How can I make 'grep' show a single line five lines above the grepped line?

查看:148
本文介绍了我怎样才能让'grep'在grepped行之上显示一行五行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前和之后都看到了一些grepping行的例子,但我想忽略中间行。
所以,我想先行五行,但没有别的。
可以这样做吗?

I've seen some examples of grepping lines before and after, but I'd like to ignore the middle lines. So, I'd like the line five lines before, but nothing else. Can this be done?

推荐答案

好的,我认为这会做你想找的。它会查找一个模式,并在每次比赛前提取第5行。

OK, I think this will do what you're looking for. It will look for a pattern, and extract the 5th line before each match.

grep -B5 "pattern" filename | awk -F '\n' 'ln ~ /^$/ { ln = "matched"; print $1 } $1 ~ /^--$/ { ln = "" }'

它需要第一行,打印它,然后等待,直到它看到 ^ - $ (grep使用的匹配分隔符),然后重新开始。

basically how this works is it takes the first line, prints it, and then waits until it sees ^--$ (the match separator used by grep), and starts again.

这篇关于我怎样才能让'grep'在grepped行之上显示一行五行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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