Hey,
I have a file and it has only one field. I need to calculate the sum of each filed as total.
For e.g my file is
1
2
3
4
5
I need to calculate the total sum as 15.
Please let me know how i can do it?
Hey,
I have a file and it has only one field. I need to calculate the sum of each filed as total.
For e.g my file is
1
2
3
4
5
I need to calculate the total sum as 15.
Please let me know how i can do it?
awk '{total+=$0}END{print total}' file
Regards
nawk '{ sum = sum + $0 } END { print sum }' file
assuming input file is as stated
# echo `tr '\n' '+' < file`0 | bc
15
Another one:
echo $(($(paste -sd+ file)))
Use ksh93 (/usr/dt/bin/dtksh on Solaris) or zsh for floating-point arithmetic.