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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding up a column of numbers 2

Status
Not open for further replies.

sjsp

Technical User
Aug 24, 2001
14
GB
Does anyone know how to add up a column of numbers, e.g kilobytes from df -k output?
 
Using awk is best if there is more than just the column as per your example, or you can use "cut" to extract the specific column and do it in the shell

x=0
for i in `du -sk * | cut -f1`
do
x=$(( x + i ))
done
echo $x

(bash or ksh, sh would use 'expr' for the math)

Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
Cool ... I didn't know about the $((....)) to math in ksh/bash.

Cheers :)

Greg.
 
Thank you, this was a useful Tek Tip :)
 
Here is the same example using awk:

du -sk * | awk '{sum += $1} END {print "Total: ", sum;}'

This allows you to tie everything into one command. Regards,
Chuck

chuck.spilman@nokia.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top