Murali Krishna
Murali Krishna

Reputation: 187

How to Filter a file using awk and sort commands based on second field,Linux?

I have a file like below which I have sorted based on the username field.

1234    200   suresh
5678    150   murali
8543    200   sanjith
5678    100   suresh
1456    400  murali

I am trying to remove the rows which have the lowest score for the same usernames. So I want to have the output using Shell Script.

1234    200   suresh
5678    400   murali
8543    200   sanjith

Upvotes: 1

Views: 234

Answers (1)

anubhava
anubhava

Reputation: 785651

You can use awk's associative array:

awk '$2>m[$3]{m[$3]=$2; r[$3]=$0} END{for (i in r) print r[i]}' file
1456    400  murali
1234    200   suresh
8543    200   sanjith

Upvotes: 1

Related Questions