如何在匹配的图案上方显示4行

时间:2016-06-04 11:01:47

标签: bash shell scripting

此处有 temp.file

文件
var c = new Company(null, null);
c.Enumerator = new ReverseEnumeration(c);

我正在使用Line no1 Line no2 Line no3 Line no4 Line no5 abc 这打印了我的线" abc"。

但是我想要打印它上面的4行......

因此,所需的输出应为

less temp.file|grep -i "abc"

怎么样,我能得到它。

1 个答案:

答案 0 :(得分:2)

GNU grep中有一个选项:-B

less temp.file | grep  -B4 -i "abc"

来自上下文线控制的手册页:

   -A NUM, --after-context=NUM
          Print NUM lines of trailing context after matching lines.  Places a line containing a group separator  (--)  between
          contiguous groups of matches.  With the -o or --only-matching option, this has no effect and a warning is given.

   -B NUM, --before-context=NUM
          Print  NUM  lines of leading context before matching lines.  Places a line containing a group separator (--) between
          contiguous groups of matches.  With the -o or --only-matching option, this has no effect and a warning is given.

   -C NUM, -NUM, --context=NUM
          Print NUM lines of output context.  Places a line containing a group separator (--)  between  contiguous  groups  of
          matches.  With the -o or --only-matching option, this has no effect and a warning is given.