【问题标题】:howto send stderr and stdout through separate grep filters? [duplicate]如何通过单独的 grep 过滤器发送标准错误和标准输出? [复制]
【发布时间】:2023-03-09 20:01:02
【问题描述】:

尝试运行产生大量标准输出和标准错误的命令。 需要一种方法来过滤大多数 stderr 消息。

类似

command 1> grep "a" 2> grep "b"

另一种方法是只 grep stderr 但也看到 stdout。

【问题讨论】:

标签: bash shell grep


【解决方案1】:

做:

{ command 2>&1 1>&3 3>&- | grep "b"; } 3>&1 1>&2 | grep "a"

注意:以上内容改编自antak's answer to "pipe stdout and stderr to two different processes in shell script?"greps 替换了其更抽象的名称。有关它的工作原理,请参阅该答案。 (尽管如此,需要grep 的懒惰编码人员可能更喜欢这样的更具体的答案。)

用于区分 stdoutstderr 的有用工具是 annotate-output,(安装 "devscripts" package),它发送 stderr 和 stdin 和 stdout 以及有用的小前缀:

annotate-output wc -c /bin/bash /bin/nosuchshell

输出:

00:29:06 I: Started wc -c /bin/bash /bin/nosuchshell
00:29:06 E: wc: /bin/nosuchshell: No such file or directory
00:29:06 O: 1099016 /bin/bash
00:29:06 O: 1099016 total
00:29:06 I: Finished with exitcode 1

可以使用sedawk,甚至是tee 和一些greps 单独解析该输出。

【讨论】:

  • grep 过滤器,虽然重新排序输出,但注释输出真的很方便。
猜你喜欢
  • 2018-08-10
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-08
  • 1970-01-01
  • 2020-08-20
相关资源
最近更新 更多