如何在另一行下方和上方(或之间)查找行

How to grep for line below another and above (or between)

我想查找包含数字 638 的行。

输出文件包含以下文本:

Please label this tape as number 1 in the arc tape sequence. 

This tape contains the following logical logs:

 638

Program over.
Done at Tue 25 Oct 2016 11:34:07 GMT

我的目标是通过 grep 查找数字 638。请注意,该数字可能因系统而异。我想在文本 "This tape contains the following logical logs:" 之后的下一行打印所需的数字行 638 或在 "Program over." 以上的 grep 得到 638.

我尝试了以下方法,但它不起作用:

grep -A "This tape contains the following logical logs" filename | 
grep -B 1 "Program over." | grep -v "Program over."

Grep -Agrep -B 由于某种原因不起作用。

我收到以下错误消息:

grep: Not a recognized flag: A

是否有可用于此的 awk 命令?

-A(之后)和 -B(之前)需要一个数字参数,如手册页中所述。如:

grep -B 1 -A 2 "This tape contains the following logical logs" filename 

这将 grep 你提到的集团。

sed -n '/logical/{n;n;p;}' ontape-level0.out

这对我有用:

sed -n '/This tape contains the following logical logs:/{n;n;p;}' ontape-level0.out'