Calculating cumulative frequency

Hi,

I have a file containing the frequency's of an element sorted in ascending order. The file looks something like this:

#Element Frequency
1 1
2 1
3 1
4 1
5 1
6 1
7 2
8 10
9 15
10 20

What I want is to calculate and print the cumulative frequency (adding the current frequency to the sum of the previous frequency's) with the existing data. So the
output file should look like this:

#Element Frequency Cumulative Frequency
1 1 1
2 1 2
3 1 3
4 1 4
5 1 5
6 1 6
7 2 8
8 10 18
9 15 33
10 20 53

Thanks !!

sum=0
while read line
do
ele=`echo "$line" | cut -f1`
freq=`echo "$line" | cut -f2`
sum=$((sum + freq))
echo "$ele $freq $sum"
done < inputfile > outputfile

thru awk..

awk '{print $1, $2, m=$2+m}' inputfile
awk '{m=(NR==1)?" Cumulative Frequency":$2+m; print $0 m}' infile
awk '{$3=c+=$2}1' infile
perl -lane 'print $_,$x+=$F[1]' your_file

tyler_durden