grep -A -B -C 输出匹配行及相邻行

发布时间:2024年01月03日

grep -A -B -C 输出匹配行及相邻行

在这里插入图片描述
grep --help 摘抄👇

文件控制:

  • -B, --before-context=数值 打印前面 <数值> 行上下文
  • -A, --after-context=数值 打印后面 <数值> 行上下文
  • -C, --context=数值 打印前后 <数值> 行上下文
文件控制:
  -B, --before-context=数值  打印前面 <数值> 行上下文
  -A, --after-context=数值  打印后面 <数值> 行上下文
  -C, --context=数值        打印前后 <数值> 行上下文

man grep摘抄

   -A NUM, --after‐context=NUM
          打印出紧随匹配的行之后的下文 NUM 行。在相邻的匹配组之间将会打印内容是 -- 的一行。

   -B NUM, --before‐context=NUM
          打印出匹配的行之前的上文 NUM 行。在相邻的匹配组之间将会打印内容是 -- 的一行。

   -C NUM, --context=NUM
          打印出匹配的行的上下文前后各 NUM 行。在相邻的匹配组之间将会打印内容是 -- 的一行。
       -A NUM, --after‐context=NUM
              打印出紧随匹配的行之后的下文 NUM 行。在相邻的匹配组之间将会打印内容是 -- 的一行。

       -B NUM, --before‐context=NUM
              打印出匹配的行之前的上文 NUM 行。在相邻的匹配组之间将会打印内容是 -- 的一行。

       -C NUM, --context=NUM
              打印出匹配的行的上下文前后各 NUM 行。在相邻的匹配组之间将会打印内容是 -- 的一行。

grep输出匹配行及之后几行用 -A 数值

-A--after-context=数值--after-context 数值,等号可有可无
打印后面 <数值> 行上下文
打印出紧随匹配的行之后的下文 NUM 行。在相邻的匹配组之间将会打印

例 打印匹配行和之后3行, 以下效果相同

grep --help | grep '\-A' -A 3
grep --help | grep -A 3 '\-A'
man grep | grep '\-A' -A 3
man grep | grep -A 3 '\-A'
grep --help | grep '\-A' --after-context=3
grep --help | grep '\-A' --after-context 3
man grep | grep '\-A' --after-context=3
man grep | grep '\-A' --after-context 3
grep --help | grep --after-context=3 '\-A'
grep --help | grep --after-context 3 '\-A'
man grep | grep --after-context=3 '\-A'
man grep | grep --after-context 3 '\-A'




grep输出匹配行及之前几行用 -B 数值

-B 数值--before-context=数值--before-context 数值,等号可有可无
打印前面 <数值> 行上下文
打印出匹配的行之前的上文 NUM 行。在相邻的匹配组之间将会打印内容是 – 的一行。

例 : 打印匹配行和之前3行, 以下效果相同

grep --help | grep '\-B' -B 3
grep --help | grep -B 3 '\-B'
man grep | grep '\-B' -B 3
man grep | grep -B 3 '\-B'
grep --help | grep '\-B' --before-context=3
grep --help | grep '\-B' --before-context 3
man grep | grep '\-B' --before-context=3
man grep | grep '\-B' --before-context 3
grep --help | grep --before-context=3 '\-B'
grep --help | grep --before-context 3 '\-B'
man grep | grep --before-context=3 '\-B'
man grep | grep --before-context 3 '\-B'




grep输出匹配行及之前几行用 -C 数值

-C 数值--context=数值--context 数值,等号可有可无
打印前后 <数值> 行上下文
打印出匹配的行的上下文前后各 NUM 行。在相邻的匹配组之间将会打印内容是 – 的一行。

例 : 打印匹配行和之前之后各3行, 以下效果相同

grep --help | grep '\-C' -C 3
grep --help | grep -C 3 '\-C'
man grep | grep '\-C' -C 3
man grep | grep -C 3 '\-C'
grep --help | grep '\-C' --context=3
grep --help | grep '\-C' --context 3
man grep | grep '\-C' --context=3
man grep | grep '\-C' --context 3
grep --help | grep --context=3 '\-C'
grep --help | grep --context 3 '\-C'
man grep | grep --context=3 '\-C'
man grep | grep --context 3 '\-C'




文章来源:https://blog.csdn.net/kfepiza/article/details/135351442
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。