在bash脚本中获取其特定给定列具有最大值的行 [英] Getting a row whose specific given column has max value in bash script

查看:82
本文介绍了在bash脚本中获取其特定给定列具有最大值的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文件(空格可能不规则)

I have file as (the spacing may not be regular)

a  34 4
b 34 5
c 4  123
d 100   34
e 1  23

现在如何使用bash脚本根据列(第二列)获取具有最大值的行. 输出应为:

Now how to get the row having the max value according to the column say, 2nd column using bash script. The output should be :

d 100   34

推荐答案

即使您所有的键值均为负,这仍然有效;如果输入文件为空,则不会打印空白行:

This will work even if all of your key values are negative and won't print a blank line if your input file is empty:

$ awk '(NR==1) || ($2 > max){max=$2; rec=$0} END{if (NR) print rec}' file
d 100   34

这篇关于在bash脚本中获取其特定给定列具有最大值的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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