linux - 打印一列中具有最高值的整行

标签 linux shell awk aix

我现在有个小问题。 我有一个包含 4 列的文件

test0000002,10030010330,c_,218
test0000002,10030010330,d_,202
test0000002,10030010330,b_,193
test0000002,10030010020,c_,178
test0000002,10030010020,b_,170
test0000002,10030010330,a_,166
test0000002,10030010020,a_,151
test0000002,10030010020,d_,150
test0000002,10030070050,c_,119
test0000002,10030070050,b_,99
test0000002,10030070050,d_,79
test0000002,10030070050,a_,56
test0000002,10030010390,c_,55
test0000002,10030010390,b_,44
test0000002,10030010380,d_,41
test0000002,10030010380,a_,37
test0000002,10030010390,d_,35
test0000002,10030010380,c_,33
test0000002,10030010390,a_,31
test0000002,10030010320,c_,30
test0000002,10030010320,b_,27
test0000002,10030010380,b_,26
test0000002,10030010320,a_,23
test0000002,10030010320,d_,22
test0000002,10030010010,a_,6

我想从第 2 列排序第 4 列的最高值。

test0000002,10030010330,c_,218 
test0000002,10030010020,c_,178 
test0000002,10030010330,a_,166 
test0000002,10030010020,a_,151 
test0000002,10030070050,c_,119 
test0000002,10030010390,c_,55 
test0000002,10030010380,d_,41 
test0000002,10030010320,c_,30 
test0000002,10030010390,a_,31 
test0000002,10030010380,c_,33 
test0000002,10030010390,d_,35 
test0000002,10030010320,a_,23 
test0000002,10030010380,b_,26 
test0000002,10030010010,a_,6

最佳答案

您的文件似乎已经在第 4 列按降序排序,因此您只需要打印第 2 列第一次出现的行:

awk -F, '!seen[$2]++' file
test0000002,10030010330,c_,218
test0000002,10030010020,c_,178
test0000002,10030070050,c_,119
test0000002,10030010390,c_,55
test0000002,10030010380,d_,41
test0000002,10030010320,c_,30
test0000002,10030010010,a_,6

如果您的输入文件未按第 4 列排序,则

sort -t, -k4nr file | awk -F, '!seen[$2]++'

关于linux - 打印一列中具有最高值的整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48530571/

相关文章:

linux - 使用linux下载SRA数据时如何解决 'ascp: "user@host :"in all sources must match'?

linux - 增加屏幕尺寸,移动虚拟屏幕

python - 将时间转换为人类可读格式的 Shell 脚本或 Python 脚本

linux - 如何通过 shell 脚本发送带有投票按钮的电子邮件?

regex - 使用正则表达式编辑电话列表模式

linux - Hive 获取超过平均值 10000 的行

regex - 使用正则表达式查找某些文件类型

bash - 使用 Ansible 创建新用户和密码

regex - awk 模式匹配和允许一个歧义/不匹配的解决方案

Linux命令用另一个字符串替换大文件中的字符串