Author: Div Grad
Subject: For each given key value, how to keep up to the first n recs
Posted: Fri Nov 11, 2016 11:38 pm (GMT 5.5)
I have a file where each input line consists of:
Country, City, Population
I want to do a sort on the file so that for each country I keep the three records of the most populous cities. Sometimes I will have less then three cities for a country to start with, sometimes more, and sometimes exactly three.
So for this input:
I would want to end up with:
I've provided the records already sorted in descending population with respect to each Country. In case it is relevant, I know that all Country names are unique and all City names are unique (ie can not occur from Country to Country). From any run to another I will not know what Countries or Cities I will encounter.
Thanks for any help and I apologize in advance if this has a simple solution, I have tried to find answer on my own but got nowhere.
Subject: For each given key value, how to keep up to the first n recs
Posted: Fri Nov 11, 2016 11:38 pm (GMT 5.5)
I have a file where each input line consists of:
Country, City, Population
I want to do a sort on the file so that for each country I keep the three records of the most populous cities. Sometimes I will have less then three cities for a country to start with, sometimes more, and sometimes exactly three.
So for this input:
Code: |
CAN CIT1 25 CAN CIT2 16 CAN CIT3 15 CAN CIT4 14 CAN CIT5 13 GBR CITA 65 GBR CITB 45 GBR CITC 12 USA CITX 66 USA CITZ 45 |
I would want to end up with:
Code: |
CAN CIT1 25 CAN CIT2 16 CAN CIT3 15 GBR CITA 65 GBR CITB 45 GBR CITC 12 USA CITX 66 USA CITZ 45 |
I've provided the records already sorted in descending population with respect to each Country. In case it is relevant, I know that all Country names are unique and all City names are unique (ie can not occur from Country to Country). From any run to another I will not know what Countries or Cities I will encounter.
Thanks for any help and I apologize in advance if this has a simple solution, I have tried to find answer on my own but got nowhere.