1

The following command format the disk_space_in_bytes variable into a human readable output, with 2 digits after the comma.

echo $disk_space_in_bytes | awk '{ suffix=" KMGT"; for(i=1; $1>1024 && i < length(suffix); i++) $1/=1024; printf("%.2f\n", $1); print(substr(suffix, i, 1)"B")}'

(Credit goes to @Innocent Bystander https://stackoverflow.com/a/36724130/6133648 for this awk command)

For example if disk_space_in_bytes is 858 458 685,44 (value in bytes), then the command will print 818.69 MB, but with a space in between.

I would like to remove this extra space, for example 818,69MB. But this get a little bit to tricky for me in awk. I tried some ORS and OFS manipulation without success.

Can you find way to remove the extra space ?

Thanks

Mago
  • 183
  • 1
  • 12
  • see also this one: https://stackoverflow.com/questions/15854332/file-size-in-human-readable-format... for example: `numfmt --to=iec-i --suffix=B --format="%.2f" 858458685.44` – Sundeep Jul 27 '17 at 16:01
  • And what have **you** tried? StackOverflow is about helping people fix their programming code. Best to show your failed attempts so we can help improve your understanding of `awk` et.al. . ***Please*** read http://stackoverflow.com/help/how-to-ask , http://stackoverflow.com/help/dont-ask , http://stackoverflow.com/help/mcve and take the [tour](http://stackoverflow.com/tour) before posting more Qs here. Good luck. – shellter Jul 27 '17 at 16:08
  • @BenjaminW. The space in the suffix is for the case when ``disk_in_space_in_bytes`` is lower than 1024 (see https://stackoverflow.com/a/36724130/6133648 comments). – Mago Jul 28 '17 at 08:23
  • @shellter As I said in the original post, I tried to set ``ORS=""`` (Output Record Separator) and ``OFS=""`` (Output Field Separator), hoping to supress the space after the printf. It didn't work and I felt that this was probably not the way to achieve that. I'll have a look at those links and keep in mind your advice for my next questions. Thanks. – Mago Jul 28 '17 at 08:31

1 Answers1

3

Just change the parameters of the awk command as shown below. Currently, it has an extra linebreak after the byte count, and it also still prints a space in " KMGT", but that space is necessary for the way that particular awk command displays the right denomination of bytes.

This command assumes you are using the gawk variant, as it uses the gawk-specific function gensub.

echo $disk_space_in_bytes | awk '{ suffix=" KMGT"; for(i=1; $1>1024 && i < length(suffix); i++) $1/=1024; printf("%.2f", $1); print(gensub(/ /, "", "g", substr(suffix, i, 1))"B")}'

Shown in pieces to make it make more sense:

echo $disk_space_in_bytes |
  awk '{
    suffix=" KMGT";
    for(i=1; $1>1024 && i < length(suffix); i++)
      $1/=1024;
    printf("%.2f", $1);
    print(gensub(/ /, "", "g", substr(suffix, i, 1))"B")
  }'

Basically, there are just two changes here:

  • Line break (\n) removed from the printf command
  • The gensub command as used here strips whitespace off of the suffix that gets appended to the byte denomination, which is a space if it is bytes, and not kilobytes or so on.
Adaline Simonian
  • 4,596
  • 2
  • 24
  • 35