AWK:保持最高值的记录,比较那些共享等领域 [英] awk: keep records with the highest value, comparing those that share other fields

查看:134
本文介绍了AWK:保持最高值的记录,比较那些共享等领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个awk脚本,保持了最高值的记录在某一领域,但只有比较共享其他两个字段的记录。

I'm trying to write an awk script that keeps the records with a highest value in a given field, but only comparing records that share two other fields.

我还是举个例子 - 这是input.txt的:

I'd better give an example -- this is the input.txt:

X A 10.00
X A 1.50
X B 0.01
X B 4.00
Y C 1.00
Y C 2.43

我想比较所有在第一共享相同值的记录和第二场(X A,X B或Y C),并选择一个与在第三场最高数值。

I want to compare all the records sharing the same value in the 1st and 2nd fields (X A, X B or Y C) and pick the one with a highest numerical value in the 3rd field.

所以,我希望这样的输出:

So, I expect this output:

X A 10.00
X B 4.00
Y C 2.43

在这个片段中,我能够挑最大值记录在第三场(但它没有考虑到previous领域,它也不它们输出):

With this snippet I am able to pick the record with max value in the 3rd field (but it's not taking into account the previous fields, and it's not outputting them either):

awk 'BEGIN {max = 0} {if ($2>max) max=$2} END {print max}' input.txt

电流(不必要的)输出:

Current (unwanted) output:

10.00

任何想法?我可以使用GAWK。

Any ideas? I can use gawk.

非常感谢事先!

推荐答案

您可以使用此AWK:

awk '{k=$1 OFS $2} $3>a[k]{a[k]=$3} END{for (i in a) print i, a[i]}' file
X A 10.00
X B 4.00
Y C 2.43

这篇关于AWK:保持最高值的记录,比较那些共享等领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆