Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何通过单独的grep过滤器发送stderr和stdout?_Bash_Shell_Grep - Fatal编程技术网

Bash 如何通过单独的grep过滤器发送stderr和stdout?

Bash 如何通过单独的grep过滤器发送stderr和stdout?,bash,shell,grep,Bash,Shell,Grep,试图运行一个生成大量stdout和stderr的命令。 需要一种方法来过滤大多数stderr消息 差不多 command 1> grep "a" 2> grep "b" 另一种方法是,既可以grep stderr,也可以查看stdout。执行以下操作: { command 2>&1 1>&3 3>&- | grep "b"; } 3>&1 1>&2 | grep "a" 注:以上内容改编自,用greps替换其更

试图运行一个生成大量stdout和stderr的命令。 需要一种方法来过滤大多数stderr消息

差不多

command 1> grep "a" 2> grep "b"
另一种方法是,既可以grep stderr,也可以查看stdout。

执行以下操作:

{ command 2>&1 1>&3 3>&- | grep "b"; } 3>&1 1>&2 | grep "a"
注:以上内容改编自,用
grep
s替换其更抽象的名称。关于它是如何工作的,请参见该答案。 (然而,需要
grep
的懒惰程序员可能更喜欢这样一个更具体的答案。)

区分stdout和stderr的一个有用的util是,(安装),它将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

可以使用
sed
awk
,甚至是
tee
和一些
grep
s来分别解析输出。

[这个答案]()尤其是彻底和有用的。grep过滤器可以对输出重新排序,注释输出非常方便。