【发布时间】:2017-05-29 15:49:58
【问题描述】:
这是Using awk, how to convert dates to week and quarter?的变体
输入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
想要的 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
因此,计算有数据的天数的季度平均值并附加结果。
2016 年第二季度的平均值计算如下:
(10+20+30+40)/2 = 50 ("2" is the number_of_unique_dates for that quarter)
2016 年第三季度的平均值为:
(50+60+70)/1 = 180
这是我正在进行的工作,似乎非常接近最终解决方案, 但不确定如何获取“唯一日期数”(第 2 栏) 并用作除数?
awk '
BEGIN { FS=OFS=";" }
NR==FNR { s[$5]+=$3; next }
{ print $0,s[$5] / need_num_of_unique_dates_here }
' output.txt output.txt
知道如何获取每季度的“唯一日期数”吗?
【问题讨论】:
-
@EdMorton 感谢您指出这一点!用字符串替换模式。
-
我从上一个问题中的输入开始,但问题变得很长,所以为了使其紧凑,我选择了上面的数据..
-
是的,我完全同意!
标签: awk pattern-matching average calculated-columns