Fshamri
Fshamri

Reputation: 1386

Reformat Linux command output

Running the following command:

root@kbl1infn1 # hpasmcli -s "show dimm" | egrep "Module|Status"

will produce:

Module #:                     1
Status:                       Ok
Module #:                     3
Status:                       Ok
Module #:                     6
Status:                       Ok
Module #:                     8
Status:                       Ok
Module #:                     1
Status:                       Ok
Module #:                     3
Status:                       Ok
Module #:                     6
Status:                       Ok
Module #:                     8
Status:                       Ok

How can I reformat the output to become like:

Module: 1  3  6  8 ...
Status: OK OK OK OK

Upvotes: 0

Views: 87

Answers (2)

codeforester
codeforester

Reputation: 42999

You can tailor two greps and cuts this way, combining with printf to format and echo to concatenate the values:

echo -e "Module: $(printf %5d $(hpasmcli -s "show dimm" | grep Module | cut -f2 -d:))\nStatus: $(printf %5s $(hpasmcli -s "show dimm" |grep Status | cut -f2 -d:))"

If you want to make a single call to hpasmcli, then:

hpasmcli -s "show dimm" > outfile
echo -e "Module: $(printf %5d $(grep Module outfile | cut -f2 -d:))\nStatus: $(printf %5s $(grep Status outfile | cut -f2 -d:))"

The output:

Module:     1    3    6    8    1    3    6    8
Status:    Ok   Ok   Ok   Ok   Ok   Ok   Ok   Ok

Upvotes: 0

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21492

Using AWK

hpasmcli -s "show dimm" | awk '
/^Module/ { m = m sprintf("%4s", $3) }
/^Status/ { s = s sprintf("%4s", $2) }
END       { print "Module:" m "\n" "Status:" s }'

Sample output:

Module:   1   3   6   8   1   3   6   8
Status:  Ok  Ok  Ok  Ok  Ok  Ok  Ok  Ok

The blocks in curly braces after regular expressions /^Module/ and /^Status/ are executed when the current record (line) matches the corresponding regular expression. The values are collected into m and s variables. At the END, both variables are printed to the standard output.

sprintf functions return the strings padded to the specified width (4).


Alternatively, split the records with a colon using -F: option. But then you will need to trim the values using gsub function, for instance:

hpasmcli -s "show dimm" | awk -F: '
/^Module/ { gsub(/ +/, "", $2); m = m sprintf("%4s", $2) }
/^Status/ { gsub(/ +/, "", $2); s = s sprintf("%4s", $2) }
END       { print "Module:" m "\n" "Status:" s }'

Using Perl

hpasmcli -s "show dimm" | perl -e '
while (<>) {
  push @m, m/:\s*(\S+)$/ if /^Module/;
  push @s, m/:\s*(\S+)$/ if /^Status/;
}
print "Module: ", join("", map { sprintf "%4s", $_ } @m), "\n";
print "Status: ", join("", map { sprintf "%4s", $_ } @s), "\n";'

The while loop reads the input line by line. If a line starts with "Module", then the value is extracted from the line using m/:\s*(\S+)$/ expression. The matching group of non-space characters (\S+) is added to @m, or @s arrays. Finally, the array items are joined and printed to the standard output.

Upvotes: 1

Related Questions