0

我正在尝试执行以下操作awk

我的数据如下所示:

REGION = ARA ---------------------------------      
Boston1/E0023        Date, Simulated
01/91   47.53815129 
02/91   50.65750607 
03/91   47.19937152 
04/91   56.61537973 
05/91   65.20709691 
06/91   41.26555885 
07/91   33.50030035 
08/91   28.53445471 
09/91   28.15509992 
10/91   42.01960281 
11/91   59.78663005 
12/91   36.73035389 
01/92   40.94643107 
02/92   39.54410249 
03/92   63.74148238 
04/92   59.17052297 
05/92   38.82535638 
06/92   44.63729125 
07/92   35.54487173 
08/92   26.69060562 
09/92   24.80315524 
10/92   56.54098106 
11/92   49.91787601 
12/92   46.36870196 
01/93   39.29539609 
02/93   52.82357496 
03/93   42.95556699 
12/12   59.29651502 
01/13   71.07555385 
02/13   67.83077701 

REGION = ARA ---------------------------------          
Eastview/EV977       Date, Simulated    
01/91   54.56       
02/91   45.67       

该数据针对不同的位置(Boston1、Eastview 等)重复,下面的行是月份(01、02 等)和年份(91、92、93 等)。在 AWK 中,我试图确定一些特定位置,例如 150 个中的 5 个,并计算特定年份(例如 1991-2012 年)第 2 列的月平均值(1 月 - 12 月)。我不确定如何一次计算所有这些。

4

2 回答 2

0

让你开始的东西:

NF==3 {
    header=$1
}

$1~/[0-9][0-9].[0-9][0-9]/{
    split($1, ym, "\/")
    month=ym[1]
    year=ym[2]
    avg[year]+=$2
    items[year]+=1      
}

/^$/ {
    print header
    for (i in avg) {
        print "year:", i, "avg:", avg[i]/items[i], "items", items[i]
    }
    delete avg
    delete items
}

输出:

$ awk -f avg.awk input
Boston1/E0023
year: 12 avg: 59.2965 items 1
year: 13 avg: 69.4532 items 2
year: 91 avg: 44.7675 items 12
year: 92 avg: 43.8943 items 12
year: 93 avg: 45.0248 items 3
于 2013-03-12T19:52:52.947 回答
0

对于所有区域或仅给定区域:

BEGIN {
    FS="[/ ]"
    OFS=","
    split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",month," ")
}
/Date/ {
    if ($1==location||location=="") {
        print $1
        for (i=1;i<=length(sum);i++)
            print month[i],sum[i]/count[i]
    }
    delete sum
    delete count
}
/^[0-9]/ {
    count[int($1)]++
    sum[int($1)]=sum[int($1)]+$5
}

所有地区:

$ tac file | awk -f script.awk
Eastview
Jan,54.56
Feb,45.67
Boston1
Jan,49.7139
Feb,52.714
Mar,51.2988
Apr,57.893
May,52.0162
Jun,42.9514
Jul,34.5226
Aug,27.6125
Sep,26.4791
Oct,49.2803
Nov,54.8523
Dec,47.4652

对于给定区域:

$ tac file | awk -v location="Eastview" -f script.awk
Eastview
Jan,54.56
Feb,45.67

$ tac file | awk -v location="Boston1" -f script.awk
Boston1
Jan,49.7139
Feb,52.714
Mar,51.2988
Apr,57.893
May,52.0162
Jun,42.9514
Jul,34.5226
Aug,27.6125
Sep,26.4791
Oct,49.2803
Nov,54.8523
Dec,47.4652
于 2013-03-12T22:04:59.370 回答