如何在 bash 的 grep 结果之前/之后获取行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12444808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:41:33  来源:igfitidea点击:

How do I fetch lines before/after the grep result in bash?

bashshellubuntu

提问by sriram

Hi I'm very new to bash programming. I want a way to search in a given Text. For that I use grepfunction:

嗨,我对 bash 编程很陌生。我想要一种在给定文本中搜索的方法。为此,我使用grep功能:

grep -i "my_regex"

That works. But given the datalike this :

那个有效。但鉴于data这样的:

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 following the word error. So my output should be:

找到单词error( using grep -i error data)后,我希望找到单词后面的 10 行error。所以我的输出应该是:

    . . . 
    . . . .
    . . . . . . 
    . . . . . . . . .
    Error data ends

Are there any ways to do it?

有什么方法可以做到吗?

回答by Jon Lin

You can use the -Band -Ato print lines before and after the match.

您可以使用-B-A前和比赛结束后印刷线条。

grep -i -B 10 'error' data

Will print the 10 lines before the match, including the matching line itself.

将打印匹配前的 10 行,包括匹配行本身。

回答by Charlotte Russell

This prints 10 lines of trailing context after matching lines

这将在匹配行后打印 10 行尾随上下文

grep -i "my_regex" -A 10

If you need to print 10 lines of leading context before matching lines,

如果需要在匹配行之前打印 10 行前导上下文,

grep -i "my_regex" -B 10

And if you need to print 10 lines of leading and trailing output context.

如果您需要打印 10 行前导和尾随输出上下文。

grep -i "my_regex" -C 10

Example

例子

user@box:~$ cat out 
line 1
line 2
line 3
line 4
line 5 my_regex
line 6
line 7
line 8
line 9
user@box:~$

Normal grep

普通的grep

user@box:~$ grep my_regex out 
line 5 my_regex
user@box:~$ 

Grep exact matching lines and 2 lines after

Grep 精确匹配行和 2 行之后

user@box:~$ grep -A 2 my_regex out   
line 5 my_regex
line 6
line 7
user@box:~$ 

Grep exact matching lines and 2 lines before

Grep 精确匹配行和 2 行之前

user@box:~$ grep -B 2 my_regex out  
line 3
line 4
line 5 my_regex
user@box:~$ 

Grep exact matching lines and 2 lines before and after

Grep 精确匹配行和前后 2 行

user@box:~$ grep -C 2 my_regex out  
line 3
line 4
line 5 my_regex
line 6
line 7
user@box:~$ 

Reference: manpage grep

参考:联机帮助页 grep

-A num
--after-context=num

    Print num lines of trailing context after matching lines.
-B num
--before-context=num

    Print num lines of leading context before matching lines.
-C num
-num
--context=num

    Print num lines of leading and trailing output context.

回答by Ray Toal

The way to do this is near the top of the man page

这样做的方法靠近手册页的顶部

grep -i -A 10 'error data'

回答by Desislav Kamenov

Try this:

尝试这个:

grep -i -A 10 "my_regex"

-A 10 means, print ten lines after match to "my_regex"

-A 10 表示,在匹配到“my_regex”后打印十行