Delete a "G" character from df -h result

Hello everybody,

I'm a new user in forum,

In a script, i would like that the result sort :

/usr - 665 Go (free: 631 Go / 6% used)

but i obtain :

/usr - 665G Go (free: 631G Go / 6% used)

My command line is :

RES_DF=`df -BG /usr/ | tail -n1 | awk -vOFS='' '{print $5, " - ", $1, " Go (free: ", $3, " Go / ",$4, " used)"}'`

Cause :
DF sort size in GO with the "G" character.

Question :
Do you have an idea for me to get what I want...?

Thank's a lot

Welcome to the forum.

Please stick to the rules (e.g. use code tags), and be careful and consistent when presentig your problem (e.g. df -h in the title while df -BG in the post).

For your problem, none of the two, be it df -h or df -BG , is the option of choice, as the results displayed may suffer from improper rounding effects, and you always ask yourself are those units for 2^30 or 1E9 multipliers. Use blocks or bytes instead, and calculate to taste.

To remove the G character, use awk 's sub() function for every field that it applies to, e.g. sub (/G/, "", $3) .
If using df -B1 , try int($3/2^30+.5)

Hi,

Ok, thanks for your help and sorry for incoherence of my title...

I almost got what i want with :

RES_DF=`df "/usr/" | grep "/usr/" | awk -vOFS='' '{print $5, " - ", $1/1024/1024, " Go (free: ", $3/1024/1024, " Go / ",$4, " used)"}'`


Reslult :  /usr/ - 4.80615 Go (free: 3.05335 Go / 34% used)
  

Except now I want to round to one number after the comma :slight_smile:

Try setting the output format string: -vOFMT="%.1f" .

1 Like

Very good Rudic, it works!

Thank a lot and have a nice weekend!

:smiley:

1 Like

I think you struggle with the GNU df output over two lines even if it's not a terminal.
A good fix is df -kP

df -kP /usr | awk '/^\// { printf "%s - %.1f Go (free: %.1f Go / %s used)\n", $6, $2/1024/1024, $4/1024/1024, $5 }'