I have multiple files with some pattern
ABCD 100
ABCD 200
EFGH 500
IJKL 50
EFGH 700
ABCD 800
IJKL 100
I want match the occurrence of each (ABCD/EFGH/IJKL) only once sorted based on highest numbers in column 2
ABCD 800
EFGH 700
IJKL 100
I tried cat *txt | sort -k 1 |
??
thanks in Advance
My bad, for not being explicit. Apologies for wasting your time. Below is detailed example. The file has multiple columns. I got the one's needed using awk and tried this cat *txt |awk '{print $3,$5}' | sort -gr |less. Now I got the strings sorted based on numeral value. Now how do I get the uniq string for the first match.
<string> <numeral>
abcde/efgh/ijkl/mnop -450.00
dfgh/adas/gfda/adasd -100.0
abcde/efgh/ijkl/mnop -100.00
lk/oiojl/ojojl -0.078
dfgh/adas/gfda/adasd 50.0
lk/oiojl/ojojl -0.150
O/p needed
abcde/efgh/ijkl/mnop -450.00
dfgh/adas/gfda/adasd -100.0
lk/oiojl/ojojl -0.150
You can use sort
twice: once to sort on the numbers, a second time to do a stable sort on the strings (so that the largest number remains first), removing duplicates to discard duplicate strings with smaller numbers.
sort -k2,2nr file.txt | sort -k1,1 -u --stable
You can use awk's associate array and then sort based on column 2:
awk '{ if ($2>arr[$1]) arr[$1]=$2} END{for (i in arr) print i, arr[i]}' file \
| sort -k2 -rn
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With