How to append stderr in same line

156 views Asked by At

I am trying to execute OS cmds on multiple host using via pssh. The problem is stderr is not printing in same line.

I am managed to print cmd output in same line but not stderr. 55

Stderr: ssh: Could, not, not known [2] 04:58:09 [], host2, /root/file1 [3] 04:58:10 [], host3, Stderr: ls: cannot, access, /root/file1: No such file or directory [4] 04:58:10 [], host4, /root/file1

Can I get similar output as below?

   [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

There are 1 answers

0
Allan On BEST ANSWER

If you consider that your stderr is already redirected to your 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"$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

if this is not the case, you will have to either use 2>&1 | or if your bash is >= to version 4 use |&