How to replace space and format output for bash script

Viewed 47

Below is hostnamectl output

   Static hostname: aztest
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: c6de1429f643409ca089t69da6b23123
           Boot ID: 9g67250bed6744t7832a1d39d00c206f
  Operating System: Red Hat Enterprise Linux 8.6 (Ootpa)
       CPE OS Name: cpe:/o:redhat:enterprise_linux:8  baseos
            Kernel: Linux 4.18.0-372.13.1.el8_6.x86_64
      Architecture: x86-64

I want this out put in below format, for a script

Static hostname   : aztest
Icon name         : computer-laptop
Chassis           : laptop
Machine ID        : c6de1429f643409ca089t69da6b23123
Boot ID           : 9g67250bed6744t7832a1d39d00c206f
Operating System  : Red Hat Enterprise Linux 8.6 (Ootpa)
CPE OS Name       : cpe:/o:redhat:enterprise_linux:8  baseos

Please help me to format this way

1 Answers

A sed (that supports the -E flag) solution/approach might be something like.

hostnamectl | sed -E 's/^([[:space:]]*)([^:]*)(.*)$/\2\1\3/'
Related