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:

#!/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;
Note: This just sorts by country and the IPs tag along, it's only intended as a refference to get you started.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.