Operating System - Linux
1748129 Members
3619 Online
108758 Solutions
New Discussion юеВ

Re: Displaying output from df -k in GB instead of KB

 
SOLVED
Go to solution
Hein van den Heuvel
Honored Contributor

Re: Displaying output from df -k in GB instead of KB

Good catch, and good fix.
My script reported Mb, not Gb.

>> I figured it out:

Excellent.

>> Too bad i can't give myself points!

Just find grab a query I created and say hello! Nah...

Seriously, I am very pleased you considered the alternative and checked out how it worked. Good work!
I had wondered whether it would be a total waste of time to add my reply, but couldn't help myself. No I'm redeemed :-).

As reward I'll show how to do grand total. :-).
I added a variable t (total) which accumulated all KB values in the main code.
Then at the end, where previously is printed just an empty line, it now prints that total.. divived by 1M.


#!/bin/sh
bdf -k -l | awk '
BEGIN {print "Oracle Filesystems\n------------------\n"}
END {printf ("\nTotal %3.1fGb\n\n", t/1024/1024)}
/ora/ && !/vg00/ && !/storage/ {
t+=$5;
printf ("%-20s is %5.1f Gb\n",$1,$5/1024/1024)
} '


Cheers,
Hein.

Dennis Handly
Acclaimed Contributor

Re: Displaying output from df -k in GB instead of KB

>How would one make this one more efficient:
df -l -k |grep total|grep -v ora|grep -v ebr|grep -v vg00|awk

If you want to do a part way job, you can combine the grep -v:
df -l -k |grep total|grep -v -e ora -e ebr -e vg00 | awk

You may have to be careful if your strings are found as substrings. You can add -w to fix that.
Patrick Ware_1
Super Advisor

Re: Displaying output from df -k in GB instead of KB

Looks like I ran into a little issue:

# df -l -k /ora05 | grep total
/ora05 (/dev/vg05/lvol1 ) : 368868648 total allocated Kb

# df -l -k /ora06 | grep total
/ora06 (/dev/vg06/lvol1) : 366767600 total allocated Kb

If you look at the middle part where the logical volume is listed on each, you can see a difference that will throw awk off. In the first listing, there is a space between the lvol name , and the closing parenthesis ")". In the second listing, there is no space. This means that the $5 is the word "total" instead of the numerical size. As a result of this, I see that the calculations are off like below due to this extra space:


/ora05 is 360223.289 Mb
/ora06 is 0.000 Mb

Do you know how to account for this?
Patrick Ware_1
Super Advisor

Re: Displaying output from df -k in GB instead of KB

I guess what I'm trying to figure out is how can I do an if, then, else type of logic to this?
Dennis Handly
Acclaimed Contributor

Re: Displaying output from df -k in GB instead of KB

>I guess what I'm trying to figure out is how can I do an if, then, else type of logic to this?

You should be able to do $(NF-3) to get that field.
$ df -l -k | grep total | awk '{print $(NF-3)}'
James R. Ferguson
Acclaimed Contributor

Re: Displaying output from df -k in GB instead of KB

Hi Patrick:

Seeing 'grep' in a pipeline to 'awk' says you are WASTING resources. 'awk' is a pattern-matching engine!

>Dennis: You should be able to do $(NF-3) to get that field.
$ df -l -k | grep total | awk '{print $(NF-3)}'

Eliminate the extra process:

# df -l -k | awk '/total/ {print $(NF-3)}'

...You even save your fingers from typing :-)

Regards!

...JRF...
Patrick Ware_1
Super Advisor

Re: Displaying output from df -k in GB instead of KB

Thank you two! I am now working on incorporating what you have showed me into what Hein van den Heuvel has provided. If anyone wants to save me some time, I wouldn't mind.
Patrick Ware_1
Super Advisor

Re: Displaying output from df -k in GB instead of KB

Ok, I figured it out:

df -k -l | awk '
/total/ && !/ora/ && !/vg00/ && !/ebr/ {
printf ("%5f\n",$(NF-3)) >> "sizes.flatfiles"
} '
Patrick Ware_1
Super Advisor

Re: Displaying output from df -k in GB instead of KB

Made another variation of Hein van den Heuvel's grand total script:

df -k -l | awk '
BEGIN {print "\nFlat Files\n------------------" >> "sizes.report"}
END {printf ("\n------------------\nTotal %3.3f Tb\n\n", t/1024/1024/1024) >> "sizes.report"}
/total/ && !/ora/ && !/vg00/ && !/ebr/ {
t+=$(NF-3);
printf ("%-35s is %5.3f Gb\n",$1,$(NF-3)/1024/1024) >> "sizes.report"
} '

df -k -l | awk '
BEGIN {print "\nOracle Filesystems\n------------------" >> "sizes.report"}
END {printf ("\n------------------\nTotal %3.3f Tb\n\n", t/1024/1024/1024) >> "sizes.report"}
/total/ && /ora/ && !/vg00/ && !/storage/ {
t+=$(NF-3);
printf ("%-35s is %5.3f Gb\n",$1,$(NF-3)/1024/1024) >> "sizes.report"
} '

df -k -l | awk '
BEGIN {print "\nEBR Filesystems\n------------------" >> "sizes.report"}
END {printf ("\n------------------\nTotal %3.3f Tb\n\n", t/1024/1024/1024) >> "sizes.report"}
/total/ && /ebr/ && !/vg00/ {
t+=$(NF-3);
printf ("%-35s is %5.3f Gb\n",$1,$(NF-3)/1024/1024) >> "sizes.report"
} '

The code above creates sizes.report

sizes.report output:
---------------------------------------------
Flat Files
------------------
/usr/local is 2.291 Gb
/usr/local2 is 0.118 Gb
/var/mqm is 9.170 Gb

------------------
Total 0.011 Tb

Oracle Filesystems
------------------
/usr/local/oracle/10.2.0 is 7.795 Gb
/usr/local/oracle/9.2.0 is 5.686 Gb
/usr/local/oracle is 5.708 Gb
/usr/local/oracle2 is 1.907 Gb

------------------
Total 0.020 Tb

EBR Filesystems
------------------
/opt/app/ebr1 is 2029.520 Gb
/opt/app/ebr2 is 2029.520 Gb
/opt/app/ebr3 is 2029.520 Gb
/opt/app/ebr4 is 2031.505 Gb
/opt/app/ebr5 is 2029.520 Gb
/opt/app/ebr6 is 2029.520 Gb

------------------
Total 11.894 Tb

---------------------------------------------
end sizes.report output


The code below processes the file, and outputs a total of the totals:

cat sizes.report | awk '
BEGIN {print "\nTotal Of All \n------------------" >> "sizes.report"}
END {printf ("\nTotal %3.3f Tb\n\n", t) >> "sizes.report"}
/Total/ {
t+=$(NF-1);
#printf ("%5.3f Tb\n",$(NF-1) >> "sizes.report")
} '

The result is appended to the end of the file sizes.report:

Total Of All
------------------

Total 11.925 Tb
Dennis Handly
Acclaimed Contributor

Re: Displaying output from df -k in GB instead of KB

>Made another variation of Hein van den Heuvel's grand total script:

Here is why you want to use grep and awk. You have three scripts that are very much alike. While you can use awk -v to pass in your report title (or do it outside), it is harder to get your RE to take a variable.

See my solution in this thread:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1158757
If you want the patterns to be variables:
awk -v p1="^003" -v p2="^abc" -v p3="^a13" -v p4="^next" '
$0 ~ p1 || $0 ~ p2 || $0 ~ p3 {

You can also have functions in awk.