Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sum disk usage from df -k with awk help

Status
Not open for further replies.

bishop7

Technical User
Jan 26, 2004
3
US
I am trying to use awk to sum the outputs from a df –k on second column.
Here is what I have so far:

df -k |nawk 'BEGIN {NR > 1 }{ sum +=$2} END {print "\n Total diskspace in Kbytes: " sum }'

The problem is that this does not seem to sum correctly. It’s missing the last row from the df output for some reason. I placed the NR >1 to skip the first line of out put from the df command. This is a title line. I don't even know if that's correct.

Any suggestions would be great.
 
[tt]
df -k|nawk 'NR>1{sum+=$2} END{print "\n Total diskspace in Kbytes: " sum}'
[/tt]
 
Thanks.. I don't really understand why taking out the BEGIN makes it work corretly.

But very big thanks
 
Due to the awk syntax being <optional expression> { processing block }
And your expression would be NR > 1.

For an introduction into awk, see faq271-5564 / Grymoire / (g)awk manual

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Just in case it might help... you can use "df -h" to return results in Megabytes (and Gigabytes) which might be easier to read... (than Kilobytes)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top