如何让“grep"在 grepped 行上方五行显示一行? [英] How can I make 'grep' show a single line five lines above the grepped line?

查看:27
本文介绍了如何让“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 '
' '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天全站免登陆