grep找到一个字符串,获取前后行,没有显示行号

Mar*_*han 3 grep

最近我需要在文件中搜索特定的字符串,我需要在匹配之前和之后显示该行.

文件

AAAA
BBBB                      
CCCC
DDDD

使用谷歌我发现以下解决方案利用grep.

grep -n1 BBBB File
Run Code Online (Sandbox Code Playgroud)

哪个输出,

1-AAAA
2-BBBB
3-CCCC
Run Code Online (Sandbox Code Playgroud)

所以这就是我想要的,除了它在输出中显示行号.

有没有办法抑制行号?

Bar*_*mar 5

用这个:

grep -C 1 BBBB input
Run Code Online (Sandbox Code Playgroud)

grep 有三个显示上下文行的选项:

  -A NUM, --after-context=NUM
          Print NUM lines of trailing context after matching lines.  Places a line  containing  --  between  contiguous  groups  of
          matches.
  -B NUM, --before-context=NUM
          Print  NUM  lines  of  leading  context  before matching lines.  Places a line containing -- between contiguous groups of
          matches.
  -C NUM, --context=NUM
          Print NUM lines of output context.  Places a line containing -- between contiguous groups of matches.
Run Code Online (Sandbox Code Playgroud)