0

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?

Markus
  • 69
  • 6

2 Answers2

1
$ cat tst.awk
BEGIN { FS=OFS=";" }
$5 != p5 { prt(); p5=$5 }
{ lines[++numLines]=$0; dates[$2]; sum+=$3 }
END { prt() }
function prt(   lineNr) {
    for (lineNr=1; lineNr<=numLines; lineNr++) {
        print lines[lineNr], sum/length(dates)
    }
    delete dates
    numLines = sum = 0
}

$ awk -f tst.awk file
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;125
f;2016-07-25;60;2016-w30;2016-q3;125
g;2016-07-25;70;2016-w30;2016-q3;125
h;2016-04-01;70;2016-w30;2016-q3;125
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
1

Another gawk solution:

awk -F';' '{ a[$5][$2]+=$3; r[NR]=$0; q[NR]=$5 }
     END { 
           for (i in a) { s=0; len=length(a[i]); 
               for (j in a[i]) { s += a[i][j] } 
               a[i]["avg"] = s/len 
           } 
           for (n=1;n<=NR;n++) { print r[n],a[q[n]]["avg"] }
     }' OFS=";" file

The output:

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

  • a[$5][$2]+=$3 - multidimensional array, summing up values for each unique date within a certain quarter

  • len=length(a[i]) - determining the number of unique dates within a certain quarter

  • for(j in a[i]){ s+=a[i][j] } - summing up values for all dates within a quater

  • a[i]["avg"]=s/len - calculating average value

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105