0

Regarding this post: Sum up numbers with KB/MB/GB/TB/PB... suffixes

I have a few machines that have older Debian version on them and can't be upgraded to a newer version of Debian, which means that the coreutils package does not contain numfmt. I tried to find a another way to get it for it (machine is Debian 7.6), but I am forced to use another way to get my disk size.

I'm currently using the following:

lshw -class disk -class storage | grep size: | cut -d "(" -f2 | cut -d ")" -f1 | sed -e 's/[^0-9]/ /g' | paste -sd+ | bc

I can easily get the size, but I am in need of getting GB/TB or even MB to it also.

If I use:

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

I get

160GB 
160GB

On my other machines I'd get for an example:

2TB
2TB
2TB

Is there a way to save the word after the numbers in a variable and later print it out?

Also, to eliminate the chance of a machine to have multiple drives with different sizes like

500GB
2TB
3TB

This way my command sadly does not work, it would give you 505.

1

I don't have the lshw command handy, so I faked some as follows:

   size: 4200KiB (4200KB)
   size: 420MiB (420MB)
   size: 42GiB (42GB)
   size: 4TiB (4TB)
   size: 2PiB (2PB)

(I found an example online and simply copied out the "size:" lines and made up some sizes).

I used awk here because once you find yourself grepping and cutting and seding, it's often easier to combine all that logic into awk.

The below awk script sets the field separators (FS) to open- and close-parenthesis, so that it's more natural to pull out the desired value. It also (redundantly) initializes the running total size (in Gb) to zero.

Each time awk sees an input line that matches the (simple) regular expression size: it begins the real work inside the braces. The value inside the parenthesis ends up in field #2, so we ask awk to match the digits in that field. We expect them to start at position 1, for some number of characters. The value is then extracted based on that length and the suffix is the remainder of the string.

We then grind through the list of possible suffixes (extend as necessary) and multiply the current size by the appropriate ratio (mentioned by Stéphane in a comment on a currently-deleted answer to be 1000-based units).

After all of the input has been consumed, awk prints the total size in GB.

Save the script into a file and run it like

lshw -class disk -class storage | awk -f /path/to/script

Script:

BEGIN {
  FS="[()]"
  sizegb=0
}

/size: / {
  match($2, "[0-9]+")
  value=substr($2, RSTART, RLENGTH)
  suffix=substr($2, RLENGTH+1)
  if (suffix == "KB")
    sizegb += value / (1000*1000)
  else if (suffix == "MB")
    sizegb += value / 1000
  else if (suffix == "GB")
    sizegb += value
  else if (suffix == "TB")
    sizegb += value * 1000
  else if (suffix == "PB")
    sizegb += value * 1000 * 1000
}

END {
  printf "Total size: %.2f GB\n", sizegb
}
  • Can i put this script into my normal script also? pastebin.com/UpYjp7Xe .. this is my script. I used the #fullspace before you gave me this answer. then i tried ur method this way but yea, i need to have this script in all of my servers, which means i'd have to copy it there, extra work. better to have it in my normal bash script, can it be done? – TheSebM8 Apr 2 '18 at 13:01
  • 1
    Sure; just single quote the whole thing as a parameter to awk. Separate commands with semicolons. – Jeff Schaller Apr 2 '18 at 13:41
  • Is there a way to get rid of rounding?. Basically right now it shows me 1200.00GB i would want it to simply show 1200GB, don't need to know after coma. – TheSebM8 Apr 3 '18 at 7:46
  • 1
    The %.2f in the printf controls that; try using %d instead. – Jeff Schaller Apr 3 '18 at 10:09

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.