当前位置: 首页 > 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.gsyq.cn/news/28649.html

相关文章:

  • Liferay Portal与DXP集合提供程序存在授权缺失漏洞分析
  • MapGIS Objects Java计算一条三维线段与一个三角形所在的平面的交点 - 教程
  • 读书笔记:OpenPBR 规范(2)
  • 项目管理软件是不是伪需求?
  • 2025内窥镜/内窥镜电缆线/B超线厂家推荐明秀电子,专业制造品质可靠
  • 2025低烟无卤/UL3302/UL3767/UL4413辐照线厂家推荐明秀电子,专业认证品质保障
  • 低代码如何成为业务与IT的沟通桥梁?破解数字化转型中的协作难题
  • 低代码如何重塑IT部门价值?
  • 2025工业冰水机/冷水机厂家推荐东莞市凯诺机械,高效制冷稳定运行
  • (例题)HTTPS 电商商品页抓包与关键数据提取
  • qoj.4878 Easy Problem 做题记录
  • LLM学习笔记DAY10
  • LLM学习笔记DAY9
  • 无需接入执行器,0 代码改造实现微服务任务调度
  • 2025 废气处理/废气治理/环保/污水/分子筛/除臭设备推荐榜:上海深城以专利技术破局,3 家企业凭场景适配登榜,助力异味治理升级
  • API 搜索的下一代形态-Apipost智能搜索:只需用业务语言描述需求,就能精准定位目标接口!
  • 2025拖鞋机/酒店拖鞋生产线厂家推荐昆仑智能,高效稳定自动化解决方案
  • [sed] replace the first line with certain info
  • 2025提升机/自动提升机厂家推荐垚林机械,高效稳定省心之选
  • 配置Modbus TCP转RS485模块读取温度内容
  • redis-配置优化
  • 2025发电机/发电机组/柴油发电机/甲醇发电机组租赁厂家推荐新疆泓浩机电,专业维修保养服务保障
  • leetcode_146 LRU缓存 - 详解
  • 2025 年压滤机厂家最新推荐排行榜:隔膜 / 污泥 / 真空 / 板框 / 带式压滤机优质品牌权威指南
  • 2025 年氮化硅陶瓷球生产厂家最新推荐榜:高精度高耐磨产品优选,国内优质企业全面剖析
  • 详细介绍:python(73) 引用.dll文件并调用函数
  • harbor基于自建证书部署HTTPS及k8s集群
  • Windows 命令行查看COM口
  • 【IEEE出版】第六届计算机通信与网络安全国际学术会议(CCNS 2025)
  • playwright自动化测试应用-Day1