Q:

Sort a CSV file by one column, allowing a specified number of duplicates in another column

I have a file containing the following information on ~23,000 cities around the world:
population:country code:city:continent
20470:ZM:Samfya:Africa
20149:ZM:Sesheke:Africa
18638:ZM:Siavonga:Africa
26459:ZW:Beitbridge:Africa
37423:ZW:Bindura:Africa
699385:ZW:Bulawayo:Africa
47294:ZW:Chegutu:Africa
61739:ZW:Chinhoyi:Africa
18860:ZW:Chipinge:Africa
28205:ZW:Chiredzi:Africa

I would like to keep only the lines which contain the four largest cities (by population) in each country.
I thought maybe I could sort by population, and then only allow a certain number of duplicates in the second column - the latter part is what I can't figure out.
Is there some way I can achieve this using e.g. awk/grep/sed/sort? 

A:

This is a rewritten solution from the over complicated solution to Vytenis.
sort -t: -k1rn file | awk -F: 'count[$2]++<5'
699385:ZW:Bulawayo:Africa
61739:ZW:Chinhoyi:Africa
47294:ZW:Chegutu:Africa
37423:ZW:Bindura:Africa
28205:ZW:Chiredzi:Africa
20470:ZM:Samfya:Africa
20149:ZM:Sesheke:Africa
18638:ZM:Siavonga:Africa

