I have a file that looks like this:
b, 20, 434
a, 20, 23
a, 10, 123
a, 20, 423
a, 10, 89
b, 20, 88
b, 10, 133
b, 10, 99
a,10)a rows together)That is, the output should be file should be
a, 10, 123
a, 20, 423
b, 10, 133
b, 20, 434
How can I do this in a bash script? Thanks for your help.
This does the job:
< input sort -k3,3gr | sort -k1,1 -k2,2g -u
It sorts numerically in reverse order on the third field and then sorts on the first and second field taking just the first occurrence (-u for unique).
You do not need padding, i.e. if you add to the input a line like
a, 3, 31
The output is:
a, 3, 31
a, 10, 123
a, 20, 423
b, 10, 133
b, 20, 434
This will slightly modify the whitespace, but perhaps that is acceptable:
awk '$3 > a[$1,$2] { a[$1,$2] = $3 } END {for( k in a) print k a[k]}' input |
    sort -n -t, -k1,1 -k2,2
But that solution is highly dependent on the whitespace in the input, so it would probably be better to do something like:
awk '$3 > a[$1","$2] { a[$1","$2] = $3 } 
    END {for( k in a) print k "," a[k]}' FS=, input |
    sort -n -t, -k1,1 -k2,2
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