如何在同一行中附加stderr

时间:2019-03-27 04:15:12

标签: bash awk sed grep pssh

我正在尝试通过pssh在多个主机上执行OS cmds。问题是stderr没有在同一行中打印。

我可以在同一行中打印cmd输出,但不能在stderr中打印。 55

Stderr:ssh:可能,不知道,不知道 [2] 04:58:09 [],host2,/ root / file1 [3] 04:58:10 [],host3, Stderr:ls:不能访问/ root / file1:没有这样的文件或目录 [4] 04:58:10 [],host4,/ root / file1

我可以得到如下类似的输出吗?

   [1] 04:41:52 [], host1, Exited with error code 255, Stderr: ssh: Could not, re
    [2] 04:41:52 [], host2, This is FILE1, Linux host2 3.0.101-0.47.52-default #1 SMP Thu Mar 26 10
    [3] 04:41:52 [], host3, Stderr: cat: /root/file1: No such file or directory, Linux host3 3.0.101-108.68-defaul
    [4] 04:41:52 [], host4, Stderr: cat: /root/file1: No such file or directory99-default64 x86_64 ..
    [5] 04:41:53 [], host5, This is FILE1 $$$$$, Linux host5 4.4.162-94.72-default #64 x86_64 x86_64 GNU/Linux

1 个答案:

答案 0 :(得分:2)

如果您认为您的stderr已经被重定向到您的stdout:

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 '!/^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,则使用|&