chaitanyak
chaitanyak

Reputation: 25

How to sort lines by deleting multiple entries in first field but keep the entry with highest value using sort and awk commands

I have a file which has two columns where the first field has multiple identical entries each with a corresponding value.

hi           3 
hello        4 
who          3 
hello        6 
hi           7 
who          9 

Now I want to sort the lines resulting in unique entries (first field) while holding the highest corresponding value from the input file. It should be something like this.

hi    7 
hello 6 
who   9 

How can I accomplish the task using sort and awk? Please help.

Upvotes: 0

Views: 45

Answers (1)

nu11p01n73R
nu11p01n73R

Reputation: 26667

I hope this is what you are looking for

awk '$2>array[$1]{array[$1]=$2} END{for (i in array) print i, array[i]}'

Test

$  cat input
hi 3
hello 4
who 3
hello 6
hi 7
who 9 

$ awk '$2>array[$1]{array[$1]=$2} END{for (i in array) print i, array[i]}' input
hello 6
hi 7
who 9

Upvotes: 1

Related Questions