查找特定日期的最大值awk [英] Finding max value of a specific date awk
问题描述
我有一个包含几行的文件,每行包含以下数据 -
I have a file with several rows and with each row containing the following data-
name 20150801|1 20150802|4 20150803|6 20150804|7 20150805|7 20150806|8 20150807|11532 20150808|12399 2015089|12619 20150810|12773 20150811|14182 20150812|27856 20150813|81789 20150814|41168 20150815|28982 20150816|24500 20150817|22534 20150818|3 20150819|4 20150820|47773 20150821|33168 20150822|53541 20150823|46371 20150824|34664 20150825|32249 20150826|29181 20150827|38550 20150828|28843 20150829|3 20150830|23543 20150831|6
name2 20150801|1 20150802|4 20150803|6 20150804|7 20150805|7 20150806|8 20150807|11532 20150808|12399 2015089|12619 20150810|12773 20150811|14182 20150812|27856 20150813|81789 20150814|41168 20150815|28982 20150816|24500 20150817|22534 20150818|3 20150819|4 20150820|47773 20150821|33168 20150822|53541 20150823|46371 20150824|34664 20150825|32249 20150826|29181 20150827|38550 20150828|28843 20150829|3 20150830|23543 20150831|6
管道分隔值表示每个日期在这个月。
每行的格式相同,列数相同。
第一列名称表示该行的唯一名称。 20150818是yyyyddmm
The pipe separated value indicates the value for each of the dates in the month. Each row has the same format with same number of columns. The first column name indicates a unique name for the row e.g. 20150818 is yyyyddmm
给定一个特定的日期,如何提取当天最大值的行的名称?
Given a specific date, how do I extract the name of the row that has the largest value on that day?
推荐答案
您不能花5秒钟给您的示例输入不同的值?无论如何,对于实际上具有不同日期值的输入,这可能会起作用:
You couldn't have taken 5 seconds to give your sample input different values? Anyway, this may work when run against input that actually has different values for the dates:
$ cat tst.awk
BEGIN { FS="[|[:space:]]+" }
FNR==1 {
for (i=2;i<=NF;i+=2) {
if ( $i==tgt ) {
f = i+1
}
}
max = $f
}
$f >= max { max=$f; name=$1 }
END { print name }
$ awk -v tgt=20150801 -f tst.awk file
name2
这篇关于查找特定日期的最大值awk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!