How to redirect output in multiple columns in bash?

Viewed 2537

I am running a test multiple times on terminal and redirecting output to a file. While redirecting I want each run as a separate column. Currently, I am able to get the following:

Run1
1
2
3
4
Run2
1
2
3
4

How to redirect it as follow:

Run1  Run2
1      1
2      2
3      3
4      4
2 Answers

With pr:

pr -2 -t -s file

or from stdin:

cat file | pr -2 -t -s

Output:

Run1    Run2
1       1
2       2
3       3
4       4

See: man pr

If command1 produces Run1 and command2 produces Run2

paste <(command1) <(command2) | column -s $'\t' -tne

will join the outputs into columns.

Here is an example with silly commands

# paste <(ls -1 /var/log/*log) <(ls -1 /var/log/*/*_log) | column -s $'\t' -tne
/var/log/alternatives.log  /var/log/cups/access_log
/var/log/auth.log          /var/log/cups/error_log
/var/log/boot.log          /var/log/cups/page_log
/var/log/bootstrap.log     
/var/log/daemon.log        
/var/log/dpkg.log          
/var/log/faillog           
/var/log/fontconfig.log    
/var/log/kern.log          
/var/log/lastlog           
/var/log/pm-powersave.log  
/var/log/pm-suspend.log    
/var/log/pycentral.log     
/var/log/syslog            
/var/log/user.log          
/var/log/vbox-install.log  
/var/log/wvdialconf.log    
/var/log/Xorg.0.log        
/var/log/Xorg.1.log        
/var/log/Xorg.2.log  

Reference: https://sleeplessbeastie.eu/2018/11/19/how-to-display-output-of-multiple-commands-using-columns/

IF you want to display AND redirect to a file, each command could include a tee that does that.

It is always good idea to include your own commands (or dummy ones) so we can give you a more specific answer.

Related