in reply to Using map function to print few elements of list returned by sort function
Note that it is also sorting the input by country in the event that the input is not grouped by country. The following is an example execution, piping your reshuffled input data (not grouped by country) into the Perl script:perl -F: -nale ' push @d, [@F]; END { print join "\n", map { join ":" +, @$_} grep { $c = $$_[1] eq $prec? $c+1:1; $prec = $$_[1]; $c>4? 0: 1} sort + {$a->[1] cmp $b->[1] || $b->[0] <=> $a->[0]} @d;};' file.txt
$ echo '20470:ZM:Samfya:Africa > 61739:ZW:Chinhoyi:Africa > 20149:ZM:Sesheke:Africa > 26459:ZW:Beitbridge:Africa > 37423:ZW:Bindura:Africa > 18638:ZM:Siavonga:Africa > 699385:ZW:Bulawayo:Africa > 47294:ZW:Chegutu:Africa > 18860:ZW:Chipinge:Africa > 28205:ZW:Chiredzi:Africa > ' | perl -F: -nale ' push @d, [@F]; > END { print join "\n", map { join ":", @$_} > grep { $c = $$_[1] eq $prec? $c+1:1; $prec = $$_[1]; $c>4? 0: 1} > sort {$a->[1] cmp $b->[1] || $b->[0] <=> $a->[0]} @d;}' 20470:ZM:Samfya:Africa 20149:ZM:Sesheke:Africa 18638:ZM:Siavonga:Africa 699385:ZW:Bulawayo:Africa 61739:ZW:Chinhoyi:Africa 47294:ZW:Chegutu:Africa 37423:ZW:Bindura:Africa
|
|---|