查找2个图案Calc Avg Print Avg AWK
我正在查找2个图案Calc Avg Print Avg AWK,awk,Awk,我正在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
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月)。我不确定如何一次计算出这一切 让你开始的东西:
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
对于所有区域或仅给定区域:
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 -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
对于给定区域:
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 -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
非常感谢-是否可以打印在某个点(可能是在开始时)获取平均值的标题?@user2100039-表示感谢的最佳方式是uppvote和accept:-)好的-但是有没有一种方法专门提取我数据样本中位置“Eastview”的数据,而不是任何其他位置的数据?我的整个数据集有许多位置。也许-如果标题==“Eastview”?除了年平均值之外,我需要91-12年的月平均值,即Eastview:一月/二月/三月/四月/五月/六月/七月/八月/九月/十月/十一月/十二月=45.6/54.7/43.3/或类似值。@user2100039请参阅更新并接受此答案,如果满足要求,请进行升级投票,我使用指定的位置运行它,但它不返回任何内容。输出应该被打印到屏幕上吗?我在tac之后用一个更好的“文件”输入文件来运行它,每个月我都会得到零。也许我能弄明白。您使用的文件格式必须与您的示例不同。看到它在这里工作了吗?您有什么
awk
吗?运行awk--version