1

I am struggling with figuring out a way to sum up a row of numbers.

I have the following code.

lshw -class disk -class storage | grep size: | cut -d "(" -f2 | cut -d ")" -f1

This gives me the result

2TB                         
2TB
2TB
2TB
2TB
2TB

On my other machine:

500GB                       
1TB
1TB

Is there a way to sum these up? I need to keep GB or TB

im totally fine it being said in GB's or a way that it only add these up if there are more than 2 results? I have multiple machines where i am supposed to run this command. Some of them have 5 Hard Drives in them, some have only 1.

2
  • Thanks to paste sample output, my lshw is not outputing like yours Mar 28 '18 at 9:43
  • Hmm, weird. thats atleast how all my machines reacted to my command, so for me it works.
    – TheSebM8
    Mar 28 '18 at 10:25
6

It may be easier with the xml or json output format provided tools to parse those are available:

lshw -quiet -class disk -class storage -xml |
  xmlstarlet sel -t -v //size -n |
  paste -sd + - |
  bc |
  numfmt --to=si --suffix=B

json:

lshw -quiet -class disk -class storage -json |
  jq '[.children[]|.size]|add' |
  numfmt --to=si --suffix=B

Even if you do not have xml/json parsing tool, outputting in those formats would still be preferable as you get an exact number of bytes to start with and so not losing information when converting from/to human readable format.

You could then revert to some heuristic-based parsing like for the default output format. With GNU grep:

lshw -quiet -class disk -class storage -json |
  grep -Po '"size" : \K\d+' |
  paste -sd + - |
  bc |
  numfmt --to=si --suffix=B

Add the --round=nearest option to GNU numfmt (in GNU coreutils since 8.21 (2013)) to round to the nearest number instead of rounding up. See also --format=%.3f to get more precision.

11
  • for these both it said "xmlstarlet and jq not found"
    – TheSebM8
    Mar 28 '18 at 10:32
  • Sure, you have to install them, but it's quite reliable then Mar 28 '18 at 10:41
  • @TheSebM8, see edit for alternative if you don't have xml/json parsing tools Mar 28 '18 at 10:49
  • is xmlstarlet tool big? and how can i get it? cause if that way its better, im willing to install it :P
    – TheSebM8
    Mar 28 '18 at 10:52
  • 1
    @TheSebM8, on a Debian-based system, you can run aptitude search -F '%I %p' '~r native !~i (^xmlstarlet$ | ~R^xmlstarlet$)' to show the install-size of the xmlstarlet package and any of its direct dependencies that are not currently installed. aptitude install xmlstarlet to install it (or apt-get install xmlstarlet if you don't have aptitude). Mar 28 '18 at 11:12
1

sed + numfmt solution:

lshw -class disk -class storage \
| sed -n '/size:/ s/.*(\([0-9]*.*[KMGT]\)B)/\1/p' \
| numfmt --from si | paste -sd+ - | bc | numfmt --to si --suffix B

paste -sd+ - | bc can be also replaced with awk '{ sum+=$1 }END{ print sum }'

3
  • for some reasons your command prints out only with T, there is no B Also, this one for an example gave me 2.8TB for the last Machine i showed.
    – TheSebM8
    Mar 28 '18 at 10:28
  • @TheSebM8, just add --suffix B , that's all Mar 28 '18 at 10:31
  • Edited my question. above. The calculations are abit off.
    – TheSebM8
    Mar 28 '18 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.