0

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
1
  • hpasmcli, I understand your situation. Good luck. Commented Jan 22, 2017 at 18:50

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.