如何使用 grep 查找匹配之前 * 和 * 之前的上下文行?
How do I find context lines before *and* after a match using grep?
我需要一次找出并输出模式前的 10 行和模式后的 20 行。
我究竟做错了什么?我没有得到正确的结果。
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.
您不会通过管道获得正确的结果,因为在通过管道输出后您将没有正确的上下文行。相反,将所有上下文要求放入同一组命令行参数中。例如:
grep -B 10 -A 20 "next" file1.txt
我需要一次找出并输出模式前的 10 行和模式后的 20 行。 我究竟做错了什么?我没有得到正确的结果。
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.
您不会通过管道获得正确的结果,因为在通过管道输出后您将没有正确的上下文行。相反,将所有上下文要求放入同一组命令行参数中。例如:
grep -B 10 -A 20 "next" file1.txt