Analyzing last 2 fields of 1 row and 3rd field of next row

I have the following script that will average the last two fields of each row, but im not sure how to include the 3rd field of the following row.

An example of the analysis that I need to perform from the input -

(66.61+58.01+54.16)/3
awk '{sum=cnt=0; for (i=13;i<=NF;i++) { sum+=$i; cnt++ } { print $1, $2, sum/cnt } }' input

Input

01001   271895  53.71   48.74   67.64   76.39   81.90   89.22   91.06   90.37   90.91   75.99   66.61   58.01
01001   271896  54.16   60.85   65.32   81.61   88.54   88.16   92.03   94.50   90.75   77.18   69.87   58.69
01001   271897  54.18   63.10   71.44   75.06   83.16   95.63   93.34   89.89   88.88   81.32   68.14   58.77
01001   271898  60.55   59.13   70.97   71.98   89.51   93.87   91.47   88.79   86.67   73.62   61.66   55.71

Output

01001 271895 62.31
01001 271896 64.28
01001 271897 63.45

Desired Output

01001 271895 59.59
01001 271896 60.91
01001 271897 62.49

Any tips?

For exactly your above laid out problem, try

awk 'NR > 1 {print LEADIN, (sum+$3)/3} {LEADIN = $1 OFS $2; sum = $13 + $14} ' file
01001 271895 59.5933
01001 271896 60.9133
01001 271897 62.4867

The output format can be modified by setting the OFMT variable.
For a two field summation, a for loop is not necessary, and you don't need a count if you're always adding three elements. If, on the other hand, you want a more generic approach, combine your attempt with this one.

2 Likes