awk-打印包含在初始分析中找到的最大值的所有行 [英] awk - Print all lines containing the Max value found in the initial analysis

查看:47
本文介绍了awk-打印包含在初始分析中找到的最大值的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下AWK命令.

(awk 'max=="" || $1 > max {max=$1; l=$0} END{ print l}' FS="|" $STAGE_FILE) < input.txt

我的目的是能够提取并打印第1列中具有最大值"的行.但是考虑下面的文件input.txt:

My intention is to be able to extract print the line that has the Max value in column 1. But considering the file input.txt below:

0.0008    6
9.0    10
9.0    19
0.7    33

我得到的输出是

9.0    19

但是我希望是

9.0    10
9.0    19

当我有两个最大值(在本例中为9)(在第一列中)时,该命令仅打印一行,而不打印所有具有9的最大值.

when I have two max values, in this case 9 (in the first column), the command only printed one of the rows and not all that have the Max value of 9.

我的代码有什么变化,可以让您打印出最大值为9的所有行?

What change in my code would allow you to print all lines with Max value of 9?

推荐答案

按照您显示的尝试,请尝试执行以下操作.

With your shown attempts, please try following.

awk '
{
  max=(max>$1?max:$1)
  arr[$1]=(arr[$1]?arr[$1] ORS:"")$0
}
END{
  print arr[max]
}
' Input_file

说明: 添加以上详细说明.

Explanation: Adding detailed explanation for above.

awk '                                 ##Starting awk program from here.
{
  max=(max>$1?max:$1)                 ##Creating max to get maximum value as $1 per line.
  arr[$1]=(arr[$1]?arr[$1] ORS:"")$0  ##Creating array arr with index of 1st field and keep adding its value to it with a new line.
}
END{                                  ##Starting END block of this program from here.
  print arr[max]                      ##Printing arr with index of max here.
}
' Input_file                          ##Mentioning Input_file name here.

这篇关于awk-打印包含在初始分析中找到的最大值的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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