If you plan on working with the data a bit, check out DBI with the DBD::CSV set. This handles all the manipulation for you allowing you to do SQL queries on the flatfile without having to worry about any direct manipulation. An excellent introduction to DBI can be found here.
Before you cheat and look at a sample answer, you might want to check out Super Search and find some refference material, the Tutorials section also has a little info. The sort manpage might be a little terse if you're just starting but play around with it and you'll have a much better understanding. The Schwartzian Transform might be of particular interest to you.
If you're going to be lots of database stuff Programming the Perl DBI has a nice intro into flatfile handling, DBM, and of course DBI.
If you've given up and want a spoiler...I'm rather new to it too so it's not all that pretty:
Note: This just sorts by country and the IPs tag along, it's only intended as a refference to get you started.#!/usr/bin/perl -w use strict; # Open the source file, take each record and split it into ip, range, # and country fields and push a ref to the anon array into our records +et my @records; open IPS, "<unsorted"; push @records, [ split/,/ ] while <IPS>; close IPS; # Perform the sort on the country field (element 2 of our anon array) my @sorted = sort { $a->[2] cmp $b->[2] } @records; # Join all the sorted pieces back together and write it to file. open SORTED, ">sorted"; print SORTED join(",", @$_) for @sorted; close SORTED;
In reply to Re: How do I sort a file with IP Addresses and another text column
by Arguile
in thread How do I sort a file with IP Addresses and another text column
by arv
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |