Shell script to sum every 3 values

I am looking for an easy way to calculate the sum of three values (*-12, *-01, *-02) which are contained after a comma (,). I have found an awk command that will sum every 3rd value, but I am not interested in the values before the comma (,).

 awk '{s+=$1}NR%3==0{print s;t+=s;s=0}'

I am only interested in summing every 3rd value after the comma (,). If it helps, this 3rd value will contain a -02 in the line.

Example:
1944-12,5.6
1945-01,9.8
1945-02,6.7
1945-03,9.3
1945-04,5.9
1945-05,0.7
1945-06,0.0
1945-07,0.0
1945-08,0.0
1945-09,0.0
1945-10,0.2
1945-11,10.5
1945-12,22.3
1946-01,35.4
1946-02,13.4

Any help is appreciated.

So what is the expected output for that sample data?

From my understanding of your question, is this what you're looking for?

awk -F, '/....-(12|01|02)/{s+=$2}END{print s}' inputfile
 TOTAL=0;for i in $(grep -- '-02,' tmp.dat | cut -d\, -f2);do TOTAL=$( echo $TOTAL + $i | bc -l  );done; echo $TOTAL

Assumes that the -02 value is present (not the case in your sample data, or you have a different meaning of every 3rd value :wink: )

Ideally, the output would be in the following format -

1945-02,22.1
1946-02,77.1

Try:

awk -F"[-,]" '$2==12{a[$1+1]+=$3}$2<3{a[$1]+=$3}END{for (i in a) print i"-02,"a}' file

Bartus

That is calculating the correct values, but it is out order. There are 'M' in some places where the values are, so that could be mucking with the script.

2005-02,2.9
2006-02,2.9
2007-02,5.1
2008-02,2.7
2009-02,1.4
1960-02,13.1
1948-02,7
1961-02,17.3
1949-02,16.8
1962-02,10.1
1963-02,3.3
1964-02,3.1
1965-02,2.9
1966-02,8.1
1967-02,0.7
1980-02,1.8
1968-02,9.3
1981-02,0
1969-02,2.6

To sort the output simply pipe it through "sort":

awk -F"[-,]" '$2==12{a[$1+1]+=$3}$2<3{a[$1]+=$3}END{for (i in a) print i"-02,"a}' file | sort -t"-" -nk1
1 Like

Thanks for the help bartus11. This script worked great for my needs.

 
awk -F"[-,]" '$2==12{a[$1+1]+=$3}$2<3{a[$1]+=$3}END{for (i in a) print i"-02,"a}' file | sort -t"-" -nk1

The above code was good for figuring the average of 3 values that have "-02" in the line.

Now I am needing the average of the 3 values that have "-03" in the line.

I thought that I could modify the code to -

 
awk -F"[-,]" '$2==01{a[$1+1]+=$3}$2<4{a[$1]+=$3}END{for (i in a) print i"-03,"a}' file | sort -t"-" -nk1

However, this will work for the for first set of values, but not the rest.

Any ideas on how to make this script output the average of n values (3) when column 2 is equal to N (-03).

The data look like -
1945-01,22.1
1945-02,77.1
1945-03,22.1
1945-04,77.1
1945-05,22.1
1945-06,77.1
1945-07,22.1
1945-08,77.1
1945-09,22.1
1945-10,77.1
1945-11,22.1
1945-12,77.1

I need the average of the 3rd column of *-01 through *-03

Thanks