简体   繁体   English

如何使用grep在*之前和之后找到上下文行?

[英]How do I find context lines before *and* after a match using grep?

I need to find out and output 10 lines before pattern and 20 lines after it at once. 我需要找出并输出模式之前的10行,以及之后的20行。 What am I doing wrong? 我究竟做错了什么? I don't get the proper result. 我没有得到正确的结果。

grep -B 10 "next" file1.txt | grep -A 20 "next" file1.txt

I need to find out and output 10 lines before pattern and 20 lines after it at once. 我需要找出并输出模式之前的10行,以及之后的20行。

You won't get the right result with a pipeline because you won't have the right context lines after piping the output. 您将无法通过管道获得正确的结果,因为在管道输出之后,您将没有正确的上下文行。 Instead, put all your context requirements into the same set of command-line arguments. 而是将所有上下文要求放入同一组命令行参数中。 For example: 例如:

grep -B 10 -A 20 "next" file1.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM