Find sum of 4 columns

Hi,

I have a file GLDATA with the fields like below.
200909,20
200908,22
200907,19
200906,25
200905,20
200904,21

I want to write a code to sum up the second column for values from 200909 to 200906, and 200908 to 200805, and 200907 to 200904, and so on. It can simply display it on the console for now.

Can it be done in very simple command instead of big script?

Regards,
Venkat

awk -F, '{A[NR]=$2; print sum+=$2-A[NR-4]; delete A[NR-4]}' GLDATA
20
42
61
86
86
85

Different solution, different results :rolleyes:

awk -F, '{a[$1]=$2}END{for(i in a){x=(((i-3) in a)?(i-3):(((i-2) in a)?(i-2):(((i-1) in a)?(i-1):"")));printf "%-15s= %s\n",(x)?i"-"x:i,a+a[(i-1)]+a[(i-2)]}}' file
200904         = 21
200905-200902  = 41
200906-200903  = 66
200907-200904  = 64
200908-200905  = 66
200909-200906  = 61

Indeed :D. It depends if e.g. 2009-2006 means including 2006 or not.

my @tmp=<DATA>;
my @t1 = map {chomp;my @tmp=split(",",$_);$tmp[1]} @tmp;
for(my $i=0;$i<=$#t1-3;$i++){
  print $t1[$i]+$t1[$i+1]+$t1[$i+2]+$t1[$i+3],"\n";
}
__DATA__
200909,20
200908,22
200907,19
200906,25
200905,20
200904,21