Reputation: 91
I have the following text in a file , it is separated by delimiter(=) , I need to get second highest value for each group of second filed , I have tried below code and able to get the highest value , is there a way we can achieve using Unix command to get the expected output?
First highest Value :
cat File.txt | awk -F= '{if (a[$2] < $1 ) a[$2] = $1;}END{for(i in a )
{print i,a[i];}' OFS='='
Input :
20180526=AA
20180530=BB
20180621=AA
20180630=BB
20180630=CC
20180630=DD
20180731=BB
20180731=CC
20180731=DD
Expected output:
20180526=AA
20180630=BB
20180630=CC
20180630=DD
Upvotes: 2
Views: 100
Reputation: 189948
You'll need to keep two arrays, and demote to the second any value from the first which gets overwritten by a higher value. Then at the end print the second array instead.
awk -F= 'a[$2] < $1 { b[$2] = a[$2]; a[$2] = $1; }
END{for (i in b) print i, b[i];}' OFS='=' file.txt
(Also notice how we avoid the useless cat
.)
This doesn't attempt to do anything useful with values which only occur once; if you can specify a behavior for those, it should be easy enough to add. (Ask a new question if you need help with that.)
Upvotes: 4