$ cat country
Chicago, USA
Frankfurt, Germany
Berlin, Germany
Washington, USA
Helsinki, Finland
New York, USA

 

 
  
  1. #!/usr/bin/perl -w 
  2. use strict; 
  3. my %table
  4. my ($city,$country); 
  5. while (<>) { 
  6.  chomp; 
  7.  my ($city,$country) = split /,/; 
  8.  push @{$table{$country}},$city; 
  9.  
  10. foreach $country (sort keys %table){ 
  11.  print "$country:"
  12.  my @cities = @{$table{$country}}; 
  13.  print join ',', sort @cities; 
  14.  print ".\n"
  15.  
  16.  

result:

$ perl country.pl country
 Finland:Helsinki.
 Germany:Berlin,Frankfurt.
 USA:Chicago,New York,Washington.