'AWK output shown multiple times (going over several times)

I have this AWK code which attemps to create several counters for several metrics. I have the counters and the variables which are the sum of the columns I need.

When I print only the first print (opponent), the output is the right one. But if add another print for another metric, on these other print, the output appears several times with slightly different result, as if the print where giving the result based on different readings....

My question is, In order to have a kind of report where I print the same format but with different variables, I have the format ok? I mean, do you see any syntax error which are leading me into having more than one print?

I will attach an image of the output to make clear the issue.

The script:

#! /usr/bin/awk -fs
BEGIN { FS="," }
NR>1  { ResultDidference[$12]++           
    Result[$11]++ 
        Opponent[$10]++
        Stadium[$9]++ 
        Year[$1]++                            
        GameScorePerYear[$1]+= $32                    
        GameScorePerStadium[$9]+= $32                    
        GameScorePerOpponent[$10]+= $32     
        GameScorePerResult[$11] += $32                
        GameScorePerResultDifference[$12] += $32              
      }
END   {print "Opponent, GameScore" #header
           for (Opp in Opponent)#Opp is the column name from the file
               printf "%s,%.2f\n", #output format
                      Opp,(GameScorePerOpponent[Opp]/Opponent[Opp]) 
                      } 
       {print "Stadium, GameScore" #header
           for (Home in Stadium) #home is the column name from the file
               printf "%s,%.2f\n", #output format
                      Home,(GameScorePerStadium[Home]/Stadium[Home]) 
                      }
       {print "Year, GameScore" #header
           for (EndYear in Year)#EndYear is the column name from the file
               printf "%s,%.2f\n", #output format
                      EndYear,(GameScorePerYear[EndYear]/Year[EndYear])
                      }

enter image description here

THis is just a screenshot, only a part of a BIG output like that, it should only be a print per for...

Some input data:

EndYear,Rk,G,Date,Years,Days,Age,Tm,Home,Opp,Win,Diff,GS,MP,FG,FGA,FG_PCT,3P,3PA,3P_PCT,FT,FTA,FT_PCT,ORB,DRB,TRB,AST,STL,BLK,TOV,PF,PTS,GmSc
1985,1,1,10/26/1984,21,252,21.6899384,CHI,1,WSB,1,16,1,40,5,16,0.313,0,0,,6,7,0.857,1,5,6,7,2,4,5,2,16,12.5
1985,2,2,10/27/1984,21,253,21.69267625,CHI,0,MIL,0,-2,1,34,8,13,0.615,0,0,,5,5,1,3,2,5,5,2,1,3,4,21,19.4
1985,3,3,10/29/1984,21,255,21.69815195,CHI,1,MIL,1,6,1,34,13,24,0.542,0,0,,11,13,0.846,2,2,4,5,6,2,3,4,37,32.9
1985,4,4,10/30/1984,21,256,21.7008898,CHI,0,KCK,1,5,1,36,8,21,0.381,0,0,,9,9,1,2,2,4,5,3,1,6,5,25,14.7
1985,5,5,11/1/1984,21,258,21.7063655,CHI,0,DEN,0,-16,1,33,7,15,0.467,0,0,,3,4,0.75,3,2,5,5,1,1,2,4,17,13.2
1985,6,6,11/7/1984,21,264,21.72279261,CHI,0,DET,1,4,1,27,9,19,0.474,0,0,,7,9,0.778,1,3,4,3,3,1,5,5,25,14.9
1985,7,7,11/8/1984,21,265,21.72553046,CHI,0,NYK,1,15,1,33,15,22,0.682,0,0,,3,4,0.75,4,4,8,5,3,2,5,2,33,29.3
1985,8,8,11/10/1984,21,267,21.73100616,CHI,0,IND,1,2,1,42,9,22,0.409,0,0,,9,12,0.75,2,7,9,4,2,5,3,4,27,21.2
1985,9,9,11/13/1984,21,270,21.73921971,CHI,1,SAS,1,3,1,43,18,27,0.667,1,1,1,8,11,0.727,2,8,10,4,3,2,4,4,45,37.5
1985,10,10,11/15/1984,21,272,21.74469541,CHI,1,BOS,0,-20,1,33,12,24,0.5,0,1,0,3,3,1,0,2,2,2,2,1,1,4,27,17.1
1985,11,11,11/17/1984,21,274,21.75017112,CHI,1,PHI,0,-9,1,44,4,17,0.235,0,0,,8,8,1,0,5,5,7,5,2,4,5,16,12.5
1985,12,12,11/19/1984,21,276,21.75564682,CHI,1,IND,0,-17,1,39,11,26,0.423,0,3,0,12,16,0.75,2,3,5,2,2,1,3,3,34,20.8
1985,13,13,11/21/1984,21,278,21.76112252,CHI,0,MIL,0,-10,1,42,11,22,0.5,0,0,,13,14,0.929,4,9,13,2,2,2,6,3,35,26.7
1985,14,14,11/23/1984,21,280,21.76659822,CHI,0,SEA,1,19,1,30,9,13,0.692,0,0,,5,6,0.833,0,4,4,3,4,1,4,4,23,19.5
1985,15,15,11/24/1984,21,281,21.76933607,CHI,0,POR,0,-10,1,41,10,24,0.417,0,1,0,10,10,1,3,3,6,8,3,1,4,4,30,23.9
1985,16,16,11/27/1984,21,284,21.77754962,CHI,0,GSW,0,-6,1,24,6,10,0.6,0,0,,1,1,1,0,2,2,3,3,2,4,1,13,11.1
1985,17,17,11/29/1984,21,286,21.78302533,CHI,0,PHO,0,-5,1,30,9,17,0.529,1,1,1,3,4,0.75,1,2,3,2,2,0,2,5,22,14
1985,18,18,11/30/1984,21,287,21.78576318,CHI,0,LAC,1,4,1,37,9,15,0.6,0,0,,2,4,0.5,2,3,5,5,3,0,4,4,20,15.5
1985,19,19,12/2/1984,21,289,21.79123888,CHI,0,LAL,1,1,1,42,7,13,0.538,0,0,,6,8,0.75,2,0,2,3,1,1,4,3,20,12.9
1985,20,20,12/4/1984,21,291,21.79671458,CHI,1,NJN,1,15,1,35,7,13,0.538,0,0,,6,6,1,1,2,3,6,1,0,3,3,20,16
1985,21,21,12/7/1984,21,294,21.80492813,CHI,1,NYK,1,2,1,43,8,16,0.5,0,1,0,5,7,0.714,1,1,2,3,2,0,6,5,21,9.3
1985,22,22,12/8/1984,21,295,21.80766598,CHI,1,DAL,1,2,1,35,10,23,0.435,0,0,,0,0,,4,3,7,2,0,2,2,3,20,11.2
1985,23,23,12/11/1984,21,298,21.81587953,CHI,1,DET,0,-7,1,37,13,28,0.464,0,1,0,1,3,0.333,1,7,8,6,2,0,3,4,27,16.2
1985,24,24,12/12/1984,21,299,21.81861739,CHI,0,DET,0,-7,1,30,6,17,0.353,0,2,0,9,10,0.9,0,1,1,2,2,1,1,5,21,12.5
1985,25,25,12/14/1984,21,301,21.82409309,CHI,0,NJN,0,-2,1,44,12,25,0.48,0,0,,10,10,1,2,6,8,8,1,0,0,4,34,29.5
1985,26,26,12/15/1984,21,302,21.82683094,CHI,1,PHI,0,-12,1,27,7,16,0.438,0,0,,0,0,,1,1,2,2,1,0,1,2,14,7.2
1985,27,27,12/18/1984,21,305,21.83504449,CHI,1,HOU,0,-8,1,45,8,20,0.4,0,1,0,2,4,0.5,1,2,3,8,3,0,1,2,18,14.5
1985,28,28,12/20/1984,21,307,21.84052019,CHI,0,ATL,1,3,1,41,12,22,0.545,0,0,,10,16,0.625,4,4,8,7,5,1,7,5,34,26.6
1985,29,29,12/22/1984,21,309,21.84599589,CHI,1,BOS,1,25,1,35,12,18,0.667,0,0,,8,8,1,1,11,12,8,0,3,3,3,32,31.7
1985,30,30,12/27/1984,21,314,21.85968515,CHI,1,CLE,1,4,1,44,20,33,0.606,0,1,0,5,5,1,2,5,7,11,3,0,3,1,45,40.1
1985,31,31,12/29/1984,21,316,21.86516085,CHI,1,ATL,0,-3,1,39,7,18,0.389,0,0,,7,9,0.778,1,3,4,5,0,0,3,6,21,10.1
1985,32,32,1/2/1985,21,320,21.87611225,CHI,0,ATL,0,-14,1,34,10,14,0.714,0,0,,5,6,0.833,1,6,7,4,3,0,4,2,25,22.3
1985,33,33,1/4/1985,21,322,21.88158795,CHI,1,MIL,1,5,1,38,9,16,0.563,0,1,0,4,5,0.8,0,2,2,6,3,0,0,4,22,20.2
1985,34,34,1/5/1985,21,323,21.8843258,CHI,0,NYK,0,-6,1,42,16,25,0.64,0,0,,10,11,0.909,4,3,7,4,2,1,3,3,42,35.5
1985,35,35,1/9/1985,21,327,21.89527721,CHI,0,BOS,0,-3,1,42,12,24,0.5,0,0,,12,13,0.923,4,2,6,6,2,1,4,4,36,28.3
1985,36,36,1/11/1985,21,329,21.90075291,CHI,1,NYK,1,16,1,30,8,14,0.571,0,0,,7,7,1,2,4,6,5,2,0,5,3,23,18.3
1985,37,37,1/12/1985,21,330,21.90349076,CHI,0,CLE,0,-3,1,36,10,21,0.476,0,1,0,4,6,0.667,4,8,12,3,1,0,3,5,24,15.8
1985,38,38,1/14/1985,21,332,21.90896646,CHI,1,DEN,1,9,1,43,11,16,0.688,0,0,,13,15,0.867,2,12,14,15,3,1,6,3,35,39.4
1985,39,39,1/16/1985,21,334,21.91444216,CHI,0,NJN,0,-6,1,44,8,17,0.471,0,1,0,11,12,0.917,1,6,7,7,2,0,6,3,27,20.1
1985,40,40,1/17/1985,21,335,21.91718001,CHI,1,CLE,1,5,1,40,8,20,0.4,0,0,,9,12,0.75,0,4,4,6,2,2,3,4,25,17.2
1985,41,41,1/19/1985,21,337,21.92265572,CHI,0,IND,0,-3,1,41,15,27,0.556,2,4,0.5,6,8,0.75,2,10,12,7,3,2,2,5,38,34
1985,42,42,1/22/1985,21,340,21.93086927,CHI,1,POR,1,8,1,39,11,20,0.55,0,1,0,7,7,1,1,11,12,5,2,0,4,4,29,23.3
1985,43,43,1/25/1985,21,343,21.93908282,CHI,1,SEA,1,17,1,37,6,15,0.4,0,1,0,10,10,1,2,2,4,8,4,2,1,3,22,24.7
1985,44,44,1/26/1985,21,344,21.94182067,CHI,1,ATL,1,13,1,43,17,24,0.708,0,0,,11,11,1,1,7,8,10,0,4,5,3,45,41.4
1985,45,45,1/29/1985,21,347,21.95003422,CHI,1,KCK,1,6,1,38,11,17,0.647,0,0,,4,6,0.667,0,7,7,7,3,0,1,3,26,25.5
1985,46,46,1/30/1985,21,348,21.95277207,CHI,0,WSB,0,-11,1,41,12,19,0.632,0,0,,14,15,0.933,0,7,7,8,4,0,4,3,38,35.6
1985,47,47,2/1/1985,21,350,21.95824778,CHI,0,PHI,0,-11,1,37,9,16,0.563,0,2,0,13,14,0.929,0,6,6,5,2,0,3,5,31,25.3
1985,48,48,2/5/1985,21,354,21.96919918,CHI,1,BOS,0,-4,1,45,14,25,0.56,0,1,0,13,14,0.929,4,8,12,7,4,1,1,6,41,40.1
1985,49,49,2/7/1985,21,356,21.97467488,CHI,0,CLE,0,-9,1,38,8,17,0.471,0,0,,7,8,0.875,3,1,4,7,4,0,7,6,23,15.8

EDIT: here is the above awk script formatted legibly by gawk -o-:

BEGIN {
        FS = ","
}

NR > 1 {
        ResultDidference[$12]++
        Result[$11]++
        Opponent[$10]++
        Stadium[$9]++
        Year[$1]++
        GameScorePerYear[$1] += $32
        GameScorePerStadium[$9] += $32
        GameScorePerOpponent[$10] += $32
        GameScorePerResult[$11] += $32
        GameScorePerResultDifference[$12] += $32
}

{
        print "Stadium, GameScore"      #header
        for (Home in Stadium) { #home is the column name from the file
                printf "%s,%.2f\n", #output format
                        Home, (GameScorePerStadium[Home] / Stadium[Home])
        }
}

{
        print "Year, GameScore" #header
        for (EndYear in Year) { #EndYear is the column name from the file
                printf "%s,%.2f\n", #output format
                        EndYear, (GameScorePerYear[EndYear] / Year[EndYear])
        }
}

END {
        print "Opponent, GameScore"     #header
        for (Opp in Opponent) { #Opp is the column name from the file
                printf "%s,%.2f\n", #output format
                        Opp, (GameScorePerOpponent[Opp] / Opponent[Opp])
        }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source