I've copied your command output to a file 'f' to make some tests, you don't need tr
and grep
, sed
alone can remove all unnecessary stuff:
$ sed -r '/^\s*$|^-|\(.*\)|^Ins/d;s/ //g' f
DesktopApplicationsModule
Registered
ServerApplicationsModule
Registered
BasesystemModule
Registered
LegacyModule
NotRegistered
Now to pretty print this lets use printf
:
$ printf '%-30s%s\n' $(sed -r '/^\s*$|^-|\(.*\)|^Ins/d;s/ //g' f)
DesktopApplicationsModule Registered
ServerApplicationsModule Registered
BasesystemModule Registered
LegacyModule NotRegistered
Looks cool) and to make it the way you wanted lets do this:
# add all data to an array
arr=( $(sed -r '/^\s*$|^-|\(.*\)|^Ins/d;s/ //g' f) )
# print initial text and first 2 items from array
printf 'Installed Products : %-30s%s\n' ${arr[@]:0:2}
# print the rest, loop over the rest of array
for ((i=2; i<${#arr[@]}; i+=2)); {
# empty valued added here⤵ to propper alighnment
printf '%21s%-30s%s\n' '' ${arr[@]:i:2}
}
Result:
Installed Products : DesktopApplicationsModule Registered
ServerApplicationsModule Registered
BasesystemModule Registered
LegacyModule NotRegistered
Update, here is the sed rule /^\s*$|^-|\(.*\)|^Ins/d;s/ //g
explanation. There are 2 sed commands separated via ';'. First /.../d
- delete command and second s///g
- substitute command. Lets take a closer look at them:
/^\s*$|^-|\(.*\)|^Ins/d
this command is consists of several regex patterns to match unwanted strings, all matched strings would be removed:
^\s*$
- will match strings of 'space' characters
^-
- will match strings starting from '-'
\(.*\)
- will match strings with '()'
^Ins
- will match strings starting with 'Ins'
s/ //g
substitute all spaces with nothing(remove spaces) I've
added this coz OP has the desired output in description without spaces.
But according to the OP's comment spaces must be preserved, lets do this. Looks like we only need to remove 2-nd sed command s/ //g
? Nope this will ruin the output:
Installed Products : Desktop Applications
Module Registered
Server Applications
Module Registered
Basesystem Module
Registered Legacy
Module Not
Registered
Instead we need to change(substitute) it to some pattern for example _SPS_
to create popper amount of items in array and then change it back to spaces. The script will be like this:
# add all data to an array
arr=( $(sed -r '/^\s*$|^-|\(.*\)|^Ins/d;s/ /_SPS_/g' f) )
arr=( "${arr[@]//_SPS_/' '}" ) # change _SPS_ back to space in whole array
# print initial text and first 2 item from array
printf 'Installed Products : %-30s%s\n' "${arr[@]:0:2}"
# print the rest, loop over the rest of array
for ((i=2;i<${#arr[@]}; i+=2)); {
# empty valued added here to propper alighnment
printf '%21s%-30s%s\n' '' "${arr[@]:i:2}"
}
Notice that now I'm wrapping ${arr[@]:i:2}
in "
this is essential coz now we've got items with spaces. And here is the output:
Installed Products : Desktop Applications Module Registered
Server Applications Module Registered
Basesystem Module Registered
Legacy Module Not Registered