Reputation: 25
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
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