Warm tip: This article is reproduced from serverfault.com, please click

How to find maximum value in a column with awk

发布于 2020-11-29 14:25:18

I have a file with two sets of data divided by a blank line:

a     3  
b     2  
c     1 

e     5   
d     8  
f     1  

Is there a way to find the maximum value of the second column in each set and print the corresponding line with awk ? The result should be:

b 3  
d 8  

Thank you.

Questioner
RIXS
Viewed
0
RavinderSingh13 2020-11-30 01:26:32

Could you please try following, written and tested based on your shown samples in 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

Explanation: Adding detailed explanation for above.

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.