Operating System - HP-UX
1751972 Members
5504 Online
108784 Solutions
New Discussion юеВ

disk space usage in % till 2 decimal places

 
SOLVED
Go to solution
sahaj bahra
Frequent Advisor

disk space usage in % till 2 decimal places

when i do df -k i get an output as:

/dev/mapper/system-scratch_lv
436797008 156839748 257769228 38% /scratch

Can i get the value as 38.xx% instead of just 38%
I need to use df -k in a script and to get the value in decimal.
7 REPLIES 7
saravanan08
Valued Contributor

Re: disk space usage in % till 2 decimal places

Hi Sonal,

Can you try with this

bdf /filesystemname | tail -1 | awk '{print $3 * 100 / $2}'

for example

# bdf /var | tail -1| awk '{print $3 * 100 / $2}'
3.40014

it will give the utilization in % with decimal
Bill Hassell
Honored Contributor
Solution

Re: disk space usage in % till 2 decimal places

Are you using HP-UX or Linux? In HP-UX, df -k looks like this:

df -k /opt
/opt (/dev/vg00/lvol6 ) : 2042016 total allocated Kb 943400 free allocated Kb
1098616 used allocated Kb

Are you referring to bdf?

bdf /opt
Filesystem kbytes used avail %used Mounted on
/dev/vg00/lvol6 2048000 1098616 943400 54% /opt

In either case, these tools (df,bdf) are hardcoded with an integer percentage. To get decimal values, use awk with printf and compute the value from the numbers:

echo "/dev/stuff 436797008 156839748 257769228 38% /scratch" | awk '{printf "%4.2f \n",$3/$2*100.0}'




Bill Hassell, sysadmin
sahaj bahra
Frequent Advisor

Re: disk space usage in % till 2 decimal places

Bill- Thanks for the solution. It worked just fine. I am using it in linux.

Can you please explain how exactly this line works.. just for my knowledge

please explain awk '{printf "%4.2f \n",$3/$2*100.0}'
Hein van den Heuvel
Honored Contributor

Re: disk space usage in % till 2 decimal places

Bill- Thanks for the solution. It worked just fine. I am using it in linux.

Can you please explain how exactly this line works.. just for my knowledge

please explain awk '{printf "%4.2f \n",$3/$2*100.0}'

Bill- Thanks for the solution. It worked just fine. I am using it in linux.

Can you please explain how exactly this line works.. just for my knowledge

please explain awk '{printf "%4.2f \n",$3/$2*100.0}'

'{}' loops over each input line and unconditionally executes the code in the block{ }

Each line is split in 'words' seperate by 'whitepace'

Printf... like the C ( perl / shell) function.

%4.2f : 4 position float with 2 characters precision.

$3 : word 3
$1 : word 2

Hein.
R.K. #
Honored Contributor

Re: disk space usage in % till 2 decimal places

Hi Sonal,

Example:
# more a1
20 30 40
10 40 800

# awk '{printf "%4.2f \n",$3/$2*100.0}' a1 <<== %4.2
133.33
2000.00

# awk '{printf "%4.3f \n",$3/$2*100.0}' a1 <<== %4.3
133.333
2000.000

In first line of file 'a1':
$2 = 30 (2nd input)
$3 = 40 (3rd input)

%4.2 --> two decimal palces
%4.3 --> three decimal palces

\n --> change line

C language works well with Awk.

More info on awk with examples:
http://www.vectorsite.net/tsawk_1.html#m1

Regds..
Don't fix what ain't broke
James R. Ferguson
Acclaimed Contributor

Re: disk space usage in % till 2 decimal places

Hi:

If you want to inline replace the percentage shown with a decimal number you can modify the 'awk' solution slightly. In this case the contents of the original line are preserved :

# echo "/dev/stuff 436797008 156839748 257769228 38% /scratch" | awk '{$5=sprintf "%4.2f",$3/$2*100.0;print}'
/dev/stuff 436797008 156839748 257769228 35.91 /scratch

The 'sprintf' function creates a string instead of printing. Here we assign the string value back to the fifth ($5) field, replacing its original value. Then, we simply print the modified line.

Regards!

...JRF...
sahaj bahra
Frequent Advisor

Re: disk space usage in % till 2 decimal places

Thankyou all..