This is a variant on Using awk, how to convert dates to week and quarter?
Input data.txt:
a;2016-04-25;10;2016-w17;2016-q2
b;2016-04-25;20;2016-w17;2016-q2
c;2016-04-25;30;2016-w17;2016-q2
d;2016-04-26;40;2016-w17;2016-q2
e;2016-07-25;50;2016-w30;2016-q3
f;2016-07-25;60;2016-w30;2016-q3
g;2016-07-25;70;2016-w30;2016-q3
Wanted output.txt:
a;2016-04-25;10;2016-w17;2016-q2;50
b;2016-04-25;20;2016-w17;2016-q2;50
c;2016-04-25;30;2016-w17;2016-q2;50
d;2016-04-26;40;2016-w17;2016-q2;50
e;2016-07-25;50;2016-w30;2016-q3;180
f;2016-07-25;60;2016-w30;2016-q3;180
g;2016-07-25;70;2016-w30;2016-q3;180
Hence, calculate the quarterly average of the days which has data and append the result.
For 2016-q2 the average is calculated as follows:
(10+20+30+40)/2 = 50 ("2" is the number_of_unique_dates for that quarter)
For 2016-q3 the average is:
(50+60+70)/1 = 180
Here is my work in progress which seem quite close to a final solution, but not sure how to get the "number of unique dates" (column 2) and use as divisor?
awk '
BEGIN { FS=OFS=";" }
NR==FNR { s[$5]+=$3; next }
{ print $0,s[$5] / need_num_of_unique_dates_here }
' output.txt output.txt
Any idea how to get the "number of unique dates" per quarter?