惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
T
Troy Hunt's Blog
P
Proofpoint News Feed
H
Help Net Security
博客园 - 叶小钗
N
Netflix TechBlog - Medium
F
Full Disclosure
Vercel News
Vercel News
C
Cyber Attacks, Cyber Crime and Cyber Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
V
V2EX
Latest news
Latest news
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Schneier on Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
News and Events Feed by Topic
M
MIT News - Artificial intelligence
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Exploit Database - CXSecurity.com
Microsoft Security Blog
Microsoft Security Blog
S
Secure Thoughts
A
About on SuperTechFans
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Spread Privacy
Spread Privacy
S
Securelist
Forbes - Security
Forbes - Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
V
Vulnerabilities – Threatpost
MyScale Blog
MyScale Blog
G
Google Developers Blog
L
Lohrmann on Cybersecurity
博客园 - Franky
T
Tor Project blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V
Visual Studio Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
Y
Y Combinator Blog

技术小黑屋

Vibe Coding 的安全风险与应对策略 - 技术小黑屋 Vibe Coding 最佳实践:10 个让开发效率提升 10 倍的关键技巧 一看就会,为 AI 编程 Agent 撸一个 MCP 服务 同样是 Sonnet 4.5,为何 CLI 工具差距这么大 定位 Android 权限声明来源 - 技术小黑屋 Android 升级 targetSDK 35 解决 namespace 问题 解决 Android Studio 关闭后终端 flutter run 进程自动结束的问题 Android 开发中的三个常见构建错误及解决方案 - 技术小黑屋 使用 flock 解决 Git `unable to read tree` 问题 幂等性的劣化:从数学确定性到 AI 不确定性 - 技术小黑屋 Could not create task ':generateDebugRFile' 问题小记 Android 模拟器实现 hosts 修改 - 技术小黑屋 Vs Code 快速实现 重写 方法 Merge(Pull) Request 推荐的标签列表 - 技术小黑屋 中国特惠!多平台广告屏蔽专家 AdGuard 买断仅需 119 元起 使用 FVM 解决 flutter 3 无法添加 uploader 问题 AAPT2 aapt2-7.2.2-7984345-osx Daemon #5: Idle daemon unexpectedly exit. This should not happen 问题解决 git clone 使用代理,实现百倍加速 - 技术小黑屋 Flutter 处理 Error Setter not found AsciiChar 问题
使用 grep 查找关键字并显示上下文行 - 技术小黑屋
androidyue · 2025-06-24 · via 技术小黑屋

背景

排查日志时,常需要定位关键字并带上一两行上下文确认语义。grep 内建的上下文选项可以直接满足需求,不必再手动 sed -n '19,21p'

快速示例

假设想在 app.log 中找出包含 Fatal error 的行,并且同时看到上一行与下一行:

1
grep -n -C 1 "Fatal error" app.log
  • -n 会显示行号,便于定位。
  • -C 1 等价于 --context=1,表示向前向后各多带 1 行。想多看几行时调整数字即可。

输出中,命中的行以冒号分隔行号与内容,上下文行则以短横线 - 连接,快速区分重点。

控制上下文范围

grep 提供三个粒度化参数:

  • -C <N>:两侧各 N 行,是最常用的形式。
  • -B <N>:只带前 N 行(Before)。
  • -A <N>:只带后 N 行(After)。

例如只关心关键字后面的调用栈,可使用:

1
grep -n -A 4 "NullPointerException" stacktrace.txt

再配合 -m 1(匹配一次后退出)可以缩短复杂日志的搜索时间。

与常见参数组合

  • -i:忽略大小写,处理大小写不一致的告警信息很方便。
  • -E:启用扩展正则,可直接写 grep -E "(WARN|ERROR)"
  • --color=auto:高亮命中关键字,在终端阅读更直观。

将这些参数组合成 Shell 函数,后续排查直接调用。例如在 ~/.bashrc 中定义:

1
2
3
4
gctx() {
  local keyword="$1" file="$2" lines="${3:-1}"
  grep -n --color=always -C "$lines" "$keyword" "$file"
}

执行 gctx "timeout" service.log 2,即可得到行号、关键字高亮、上下文行的结果。

小结

  • -C/-A/-B 是获取上下文的核心选项,记住数字表示行数即可。
  • 搭配 -n--color-m 等参数可以提升排查效率。
  • 如果命中结果过多,将命令与 less -Rfzf 管道组合,能够在终端中进行二次筛选,让排查体验更顺滑。