gpt4 book ai didi

database - 如何使用awk查找列中的最大值

转载 作者:行者123 更新时间:2023-12-02 02:30:50 24 4
gpt4 key购买 nike

我有一个文件,其中包含由空行分隔的两组数据:

a     3  
b 2
c 1

e 5
d 8
f 1

有没有办法找到每组中第二列的最大值并用 awk 打印相应的行?结果应该是:

b 3  
d 8

谢谢。

最佳答案

您能否尝试按照您在 GNU awk 中显示的示例进行编写和测试。

awk '
!NF{
if(max!=""){ print arr[max],max }
max=""
}
{
max=( (max<$2) || (max=="") ? $2 : max )
arr[$2]=$1
}
END{
if(max!=""){ print arr[max],max }
}
' Input_file

说明:为上述内容添加详细说明。

awk '                            ##Starting awk program from here.
!NF{ ##if NF is NULL then do following.
if(max!=""){ print arr[max],max } ##Checking if max is SET then print arr[max] and max.
max="" ##Nullifying max here.
}
{
max=( (max<$2) || (max=="") ? $2 : max ) ##Checking condition if max is greater than 2nd field then keep it as max or change max value as 2nd field.
arr[$2]=$1 ##Creating arr with 2nd field index and 1st field as value.
}
END{ ##Starting END block of this program from here.
if(max!=""){ print arr[max],max } ##Checking if max is SET then print arr[max] and max.
}
' Input_file ##mentioning Input_file name here.

关于database - 如何使用awk查找列中的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65061153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com