1

i wrote the following bash script

#!/bin/bash
serverlist=server_list.txt

for server in `cat $serverlist`
 do
out="hostnamectl | egrep 'Kernel|Server|hostname';who -b"
ssh -q $server $out

and I get the following output ~

Static hostname: mshost25 
 Operating System: Red Hat Enterprise Linux Server 7.9 (Maipo)
 Kernel: Linux 3.10.0-1160.31.1.el7.x86_64
 system boot  2021-06-24 11:42

I would like the output to be in columns

Static hostname    Operating System             Kernel:                             sytem boot
 mypc1               Linux Server 7.9        Linux 3.10.0-1160.31.1.el7.x86_64    2021-06-24 11:33
Jetchisel
  • 7,493
  • 2
  • 19
  • 18
  • 4
    What is your definition of "columns"? What code have you tried to write for transforming the output? – jhnc Jun 30 '21 at 23:36
  • I think a python script will be much better suited for this task – ant1g Jul 01 '21 at 07:39
  • 1
    There are many, many, many questions about transforming a matrix of cells in a tab-delimited file, for example. If you can do the extraction part so you have the strings you want in a sequence, transforming that to a row should be easy enough to google. – tripleee Jul 01 '21 at 09:38
  • Tangentially, [don't read lines with `for`](http://mywiki.wooledge.org/DontReadLinesWithFor) – tripleee Jul 01 '21 at 09:38

1 Answers1

1

here is my attempt to print in columns and worked for me. Can you try this code and inform me?

I treated your output as output.txt;

$ cat output.txt 
Static hostname: mshost25 
 Operating System: Red Hat Enterprise Linux Server 7.9 (Maipo)
 Kernel: Linux 3.10.0-1160.31.1.el7.x86_64
 system boot  2021-06-24 11:42

here is the code;

sed 's/boot/boot:/' output.txt | awk -F": " '
    { 
        for (i=1; i<=NF; i++)  {a[NR,i] = $i}
    }
    NF>p { p = NF }
    END { for(j=1; j<=p; j++) {
            str=a[1,j]
            for(i=2; i<=NR; i++) {str=str"\t\t"a[i,j];} print str}
    }' | column -s $'\t' -t

my output for this code;

Static hostname     Operating System                               Kernel                               system boot
mshost25           Red Hat Enterprise Linux Server 7.9 (Maipo)    Linux 3.10.0-1160.31.1.el7.x86_64     2021-06-24 11:42

Code is basically consists of three section:

  1. sed part is used to add : after word boot in order to further process it as a delimiter in awk section. https://askubuntu.com/a/879323
  2. awk this is basically a copy paste from https://stackoverflow.com/a/1729980/14320738 so refer to this post.
  3. column command is used just for beautiful appearance. refer here => https://unix.stackexchange.com/a/448558/434132
  • hi Ahmet. thanks for your response. i tried running your code but i get an error. sed: -e expression #1, char 14: unknown option to `s'. Also is there a way to integrate with my script – Arturo Herandez Jul 02 '21 at 02:35
  • I don't get that error but there are posts related to your problem https://unix.stackexchange.com/q/91630/434132 you may need to replace "/" with "|" character. Also, did you created file "output.txt" where you write your given output? – Ahmet Said Akbulut Jul 02 '21 at 06:24