如何用年计算特定列月份的平均值?

时间:2017-06-10 06:46:28

标签: awk

我的CSV文件的温度数据为<li class="list-group-item" ng-repeat="link in links | filter:cat.category"></li>April个月,为期六年。我想用每年计算每个月的平均值。我正在使用May命令,但它显示0输出。我不知道如何使用awk命令来解决这个问题。我正在使用此脚本来解决问题;

awk

输入片段:

  $ mawk ' NR>1 { split($5,d,"/"); s[d[3]"/"d[1]]+=$6; c[d[3]"/"d[1]]++;
  } END { for (i in s) print i, s[i]/c[i]; }' DELHI_IMD1618.csv

我想要这样的输出

Stn ID Lat     Long time(GMT) DATE TEMPERATURE
IMDE1615_14164F(NAJAFGARH),28.63,77.01,2,04/01/2012,32
IMDE1615_14164F(NAJAFGARH),28.63,77.01,15,04/04/2012,32.7
IMDE1615_14164F(NAJAFGARH),28.63,77.01,6,05/01/2012,35.2
IMDE1615_14164F(NAJAFGARH),28.63,77.01,7,05/01/2012,36
IMDE1615_14164F(NAJAFGARH),28.63,77.01,8,05/01/2012,36.1
IMDE1615_14164F(NAJAFGARH),28.63,77.01,0,04/02/2013,28.7
IMDE1615_14164F(NAJAFGARH),28.63,77.01,1,04/02/2013,28.6

2 个答案:

答案 0 :(得分:0)

gawk 解决方案:

awk -F, 'NR>1{ split($5,a,"/"); k=a[1]"/"a[3]; data[k]["c"]++;  data[k]["s"]+=$6 }
         END{ for(i in data) print i,data[i]["s"]/data[i]["c"] }' DELHI_IMD1618.csv

示例输出(对于您的输入片段):

05/2012 35.7667
04/2012 32.35
04/2013 28.65

详细

  • -F, - 字段分隔符

  • split($5,a,"/") - 按/分割“ date ”列

  • k=a[1]"/"a[3] - 形成关联数组的复合键(即"04/2012"

  • data[k]["c"]++ - 增加分组项目的数量("c" - “count”)

  • data[k]["s"]+=$6 - 累积温度列值

  • data[i]["s"]/data[i]["c"] - 计算平均值

答案 1 :(得分:0)

$ cat tst.awk
BEGIN { FS="[,/]" }
NR==1 { next }
{ date=$5"/"$7 }
date != prev { prtMean(); prev=date }
{ sum+=$8; cnt++ }
END { prtMean() }

function prtMean() {
    if (cnt) {
        print prev, sum / cnt
        sum = cnt = 0
    }
}

$ awk -f tst.awk file
04/2012 32.35
05/2012 35.7667
04/2013 28.65