当前位置: 首页 > news >正文

[grep] grep stream 2, the error message

 

In Unix-like systems, stdout (standard output) is stream 1, and stderr (standard error) is stream 2.
By default, grep reads from stdin, which typically receives stdout — not stderr.

To have grep search the error stream (stderr) instead of stdout, you must redirect stderr (2) into stdin (0), like this:

command 2>&1 >/dev/null | grep "pattern"

Let’s break that down precisely:

  • command — the command whose stderr you want to inspect.

  • 2>&1 — redirects stderr (2) to stdout (1).

  • >/dev/null — discards the original stdout so that only stderr ends up going into the pipe.

  • | grep "pattern" — filters the combined (now pure stderr) stream for the given text pattern.

Example

gcc bad_code.c 2>&1 >/dev/null | grep "error"

This will show only error messages (from stderr) that contain the word “error”.


If you want to only redirect stderr to grep (without destroying stdout), the canonical, cleaner form is:

command 2> >(grep "pattern")

This uses process substitution (a Bash feature) to send just the stderr stream to grep, leaving stdout untouched.

Example

gcc bad_code.c 2> >(grep "warning")

grep filters warnings from stderr, while stdout (if any) still prints normally.


Summary of key variants

PurposeCommand
Search stderr only, discard stdout `command 2>&1 >/dev/null
Search stderr only, keep stdout intact command 2> >(grep "pattern")
Search both stdout and stderr together `command 2>&1

Would you like me to show how to capture the filtered stderr lines in a file as well (for logging or diagnostics)?

 

http://www.jsqmd.com/news/20641/

相关文章:

  • P5285 [十二省联考 2019] 骗分过样例
  • Liferay Portal与DXP集合提供程序存在授权缺失漏洞分析
  • MapGIS Objects Java计算一条三维线段与一个三角形所在的平面的交点 - 教程
  • layui时间与日期选择器,时间范围查询数据,后端springboot
  • 读书笔记:OpenPBR 规范(2)
  • 轻量级图片信息解析程序
  • 2025.10.23 闲话-全局位运算 max 的解法
  • express 模块学习 - 东方不败-
  • 习题-无限集与选择公理
  • Error: [WinError 10013] 以一种访问权限不允许的方式做了一个访问套接字的尝试及其解决方法
  • 题解:CF2115F1 Gellyfish and Lycoris Radiata (Easy Version)
  • 项目管理软件是不是伪需求?
  • 2025内窥镜/内窥镜电缆线/B超线厂家推荐明秀电子,专业制造品质可靠
  • 2025低烟无卤/UL3302/UL3767/UL4413辐照线厂家推荐明秀电子,专业认证品质保障
  • 2025.10.23考试记录
  • 低代码如何成为业务与IT的沟通桥梁?破解数字化转型中的协作难题
  • 2025铁氟龙/极细铁氟龙/UL系列高温线厂家推荐明秀电子,专业耐用品质保障!
  • LIS 略解
  • 低代码如何引爆全员创新?揭秘技术民主化背后的蒲公英效应
  • 低代码如何重塑IT部门价值?
  • 2025工业冰水机/冷水机厂家推荐东莞市凯诺机械,高效制冷稳定运行
  • 2025小型低温/工业/风冷/一体式螺杆冷冻机厂家推荐:东莞凯诺机械专业制冷解决方案
  • 2025水冷螺杆/风冷螺杆冷水机厂家推荐东莞市凯诺机械,高效制冷稳定可靠
  • (例题)HTTPS 电商商品页抓包与关键数据提取
  • qoj.4878 Easy Problem 做题记录
  • LLM学习笔记DAY10
  • 日志级别
  • noipd8t2 - Slayer
  • OJ模拟面试3(异步判题架构)
  • Edge浏览器网页设置深色模式(仅搜索结果界面)