grep 搜索日志,如何同时显示匹配结果的前后 10 行

由于日志文件很大,只好用 grep 搜索系统日志定位错误信息,但是 grep 默认展示的匹配的当前行。

我希望能展示当前行,以及之前的 10 行及之后的 10 行,有什么解决方法吗?

阅读 9.3k
2 个回答
grep -A 10 -B 10 pattern content

-A 是匹配行之前的行数,-B 是之后的行数。

  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进