如何在 bash 的 grep 结果之前/之后获取行? [英] How do I fetch lines before/after the grep result in bash?
本文介绍了如何在 bash 的 grep 结果之前/之后获取行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一种在给定文本中进行搜索的方法.为此,我使用 grep
:
grep -i "my_regex";
那行得通.但鉴于这样的数据:
这是测试数据这是错误数据如下......................错误数据结束
一旦我找到单词error
(使用grep -i error data
),我希望找到单词error
后面的10 行>.所以我的输出应该是:
有什么办法吗?
解决方案
您可以使用 -B
和 -A
打印匹配前后的行.
grep -i -B 10 '错误'数据
将打印匹配前的 10 行,包括匹配行本身.
I want a way to search in a given text. For that, I use grep
:
grep -i "my_regex"
That works. But given the data like this:
This is the test data
This is the error data as follows
. . .
. . . .
. . . . . .
. . . . . . . . .
Error data ends
Once I found the word error
(using grep -i error data
), I wish to find the 10 lines that follow the word error
. So my output should be:
. . .
. . . .
. . . . . .
. . . . . . . . .
Error data ends
Are there any way to do it?
解决方案
You can use the -B
and -A
to print lines before and after the match.
grep -i -B 10 'error' data
Will print the 10 lines before the match, including the matching line itself.
这篇关于如何在 bash 的 grep 结果之前/之后获取行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文