in reply to How do I sort a file with IP Addresses and another text column?

I'm not totally clear as to what you want to sort by. But the example below will sort on the first IP address of every line. If you want to sort by the country, it's even easier, modify as necessary (see by_country for an example).
open(F, $ARGV[0]) or die "Couldn't open file\n"; print sort by_ip <F>; close(F); sub by_ip { my ($aip) = split /,/, $a; my ($bip) = split /,/, $b; my ($aa, $ab, $ac, $ad) = split /\./, $aip; my ($ba, $bb, $bc, $bd) = split /\./, $bip; return ($aa <=> $ba) || ($ab <=> $bb) || ($ac <=> $bc) || ($ad <=> $ +bd); } sub by_country { my ($ac) = (split /,/, $a)[2]; my ($bc) = (split /,/, $b)[2]; return ($ac cmp $bc); }

Originally posted as a Categorized Answer.

  • Comment on Re: How do I sort a file with IP Addresses and another text column
  • Download Code