awk '{ sum += $1 } END { print sum }' myfile.txt
Add up first column, print sum and average:
{ s += $1 }
END { print "sum is", s, " average is", s/NR }
awk '{ s += $1 } END { print "sum is", s, " average is", s/NR }' myfile.txt
Probably not a perfect solution (I always use sed(), not awk() (-; ):Would it be possible add up a mix of MB and KB?
sed 's/[^0-9]*\([0-9]*\)[kK][bB]*.*/\1\*1024/; s/[^0-9]*\([0-9]*\)[mM][bB]*.*/\1\*1024^2/' my_file | bc | echo 0 `sed 's/^/+/'` | bc
I like using sed(), although not clever enough for scripts like that...
I just pasted it straight in from your post.bc: stdin:1: illegal character: : unexpected
bc: stdin:1: illegal character: : unexpected
-c
option. Also, you won't have to worry about KB and MB suffixes if you don't use the -h
option, and look at the -k
option.It's a usage report from my ISP. My allowance is 10GB, so I would guess it's 1.000.000 bytes.When your input file says "MB", does that mean 1.000.000 bytes or 1.048.576 bytes?
A legacy spinning disk has 512 byte sectors so your allowance is probably an even multiple of that.It's a usage report from my ISP. My allowance is 10GB, so I would guess it's 1.000.000 bytes.