如何在同一行追加 stderr

How to append stderr in same line

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

我设法在同一行打印 cmd 输出,但不是 stderr。 55

Stderr: ssh: 可以,不,未知 [2] 04:58:09 [], host2, /root/file1 [3] 04:58:10 [], host3, Stderr: ls: cannot, access, /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

如果您认为您的 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:

awk '!/^Stderr:/{if(tmp){tmp=tmp"\n"[=11=]}else{tmp=[=11=]};next}{print tmp", "[=11=]; 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 使用 |&