Hi,
I have the below text:
16:00 0.50
16:00 0.30
16:00 0.00
16:00 0.00
16:00 0.30
16:01 0.00
16:01 0.30
I want to find the max of the 2nd column grouping by the values in the 1st column using awk. So
16:00 0.50
16:01 0.30
I have tried
awk -F" " '{ if(!($1 in max) || (max[$1]<$3)) max[$1]=$3 ; count[$1]++ } END { for(element in max) printf("%s %.2f\n", element, max[element] ) } ' file.txt
but all i get is
16:00 0.00
15:59 0.00
16:01 0.00
even
cat file.txt | awk -F " " 'BEGIN{if(a[$1]<$2) { a[$1]=$3;}}END {for(i in a){print i " " a;}}'
does not work...
Any help much appreciated.
Yoda
2
A BEGIN
rule is invoked even before the first record in a file is read. So never put any code over there which deals with input file records:
awk '
{
if ( A[$1] < $2 )
A[$1] = $2
}
END {
for ( k in A )
print k, A[k]
}
' file
1 Like
Hello,
Following is one more approach for same.
awk 'NR==FNR{a[$1]=$2;} ($1 in a){if(a[$1]<$2) {a[$1]=$2}} END{for(j in a){print j OFS a[j]}}' check_2nd_column_max_value12111 check_2nd_column_max_value12111
Output will be as follows.
16:00 0.50
16:01 0.30
NOTE: Where check_2nd_column_max_value12111 is the input file name.
Thanks,
R. Singh
awk '{if($2 > a[$1]) {a[$1] = $2}} END {for(x in a) {print x, a[x]}}' file
sorry, I didn't see Yoda's solution...I have provided the same solution