我想在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月)。我不确定如何一次性计算这一切。
答案 0 :(得分: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
答案 1 :(得分: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