0

I have a unix file with the following contents.

$cat myfile.txt
abc:1
abc:2
hello:3
hello:6
wonderful:1
hai:2
hai:4
hai:8

How do I get the max value given for each text in the file above.

'abc' value 2 'hello' value 6 'hai' value 8 'womderful' 1

4

1 回答 1

2

Based on the current example in your question, minus the first line of expected output:

awk -F':' '{arr[$1]=$2 ; next} END {for (i in arr) {print i, arr[i]} } ' inputfile

You example input and expected output are very confusing.... The reason I posted this is to get feedback from the OP forthcoming

This assumes the data is unsorted, but also works with sorted data (New):

sort -t: -k2n inputfile | awk -F':' '{arr[$1]=$2 ; next} END {for (i in arr) {print i, arr[i]} } '
于 2012-11-17T01:22:00.637 回答