在bash中第一列的每个不同值中找到第n列的最大值 [英] Find the maximum values in nth column for each distinct values in 1st column in bash

查看:71
本文介绍了在bash中第一列的每个不同值中找到第n列的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3列文件,我想找到第三列的最大值,其中第一行具有相同的行,并且在输出中也包含第二列.

I have a 3 column file and I want to find the maximum value of the third column with rows with same first column and have also the second column in output.

输入:

1   234   0.005
1   235   0.060
1   236   0.001
2   234   0.010
2   235   0.003
2   236   0.003
3   234   0.004
3   235   0.100
3   236   0.004

所需的输出:

1   235   0.060
2   234   0.010
3   235   0.100

我从以前的问题中找到了这个提示,但是我也不知道第二栏:

I found this hint from previous questions but I do not know how to have also the second column:

!($1 in max) || $3>max[$1] { max[$1] = $3 }
END {
     PROCINFO["sorted_in"] = "@ind_num_asc"
     for (key in max) {
         print key, max[key]
         }
     }

推荐答案

您可以使用以下awk:

awk '!($1 in max) || $3 > max[$1] { max[$1] = $3; two[$1] = $2 }
END { PROCINFO["sorted_in"] = "@ind_num_asc"
   for (i in max) print i, two[i], max[i]
}' file

1 235 0.060
2 234 0.010
3 235 0.100

这篇关于在bash中第一列的每个不同值中找到第n列的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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