如果您认为您的标准错误已被重定向到您的标准输出:
cat stderr.in
[1] 04:41:52 [FAILURE], host1, Exited with error code 255
Stderr: ssh: Could, not, resolve hostname host1: Name or service not known
[2] 04:41:52 [SUCCESS], host2, This is FILE1, Linux host2 3.0.101-0.47.52-default #1 SMP Thu Mar 26 10:55:49 UTC 2015 (0e3c7c8) x86_64 x86_64 x86_64 GNU/Linux
[3] 04:41:52 [SUCCESS], host3, , Linux host3 3.0.101-108.68-default #1 SMP Mon Aug 13 18:53:23 UTC 2018 (ef94234) x86_64 x86_64 x86_64 GNU/Linux
Stderr: cat: /root/file1:, No, such file or directory
[4] 04:41:52 [SUCCESS], host4, , Linux host4 3.12.74-60.64.99-default #1 SMP Tue Aug 14 07:11:35 UTC 2018 (d28148a) x86_64 x86_64 x86_64 GNU/Linux
Stderr: cat: /root/file1:, No, such file or directory
[5] 04:41:53 [SUCCESS], host5, This is FILE1 $$$$$, Linux host5 4.4.162-94.72-default #1 SMP Mon Nov 12 18:57:45 UTC 2018 (9de753f) x86_64 x86_64 x86_64 GNU/Linux
awk:
awk '!/^Stderr:/{if(tmp){tmp=tmp"\n"$0}else{tmp=$0};next}{print tmp", "$0; tmp=""}END{print}' stderr.in
[1] 04:41:52 [FAILURE], host1, Exited with error code 255, Stderr: ssh: Could, not, resolve hostname host1: Name or service not known
[2] 04:41:52 [SUCCESS], host2, This is FILE1, Linux host2 3.0.101-0.47.52-default #1 SMP Thu Mar 26 10:55:49 UTC 2015 (0e3c7c8) x86_64 x86_64 x86_64 GNU/Linux
[3] 04:41:52 [SUCCESS], host3, , Linux host3 3.0.101-108.68-default #1 SMP Mon Aug 13 18:53:23 UTC 2018 (ef94234) x86_64 x86_64 x86_64 GNU/Linux, Stderr: cat: /root/file1:, No, such file or directory
[4] 04:41:52 [SUCCESS], host4, , Linux host4 3.12.74-60.64.99-default #1 SMP Tue Aug 14 07:11:35 UTC 2018 (d28148a) x86_64 x86_64 x86_64 GNU/Linux, Stderr: cat: /root/file1:, No, such file or directory
[5] 04:41:53 [SUCCESS], host5, This is FILE1 $$$$$, Linux host5 4.4.162-94.72-default #1 SMP Mon Nov 12 18:57:45 UTC 2018 (9de753f) x86_64 x86_64 x86_64 GNU/Linux
如果不是这种情况,您将不得不使用2>&1 |
,或者如果您的 bash >= 到版本 4,则使用|&