arv has asked for the wisdom of the Perl Monks concerning the following question:

I am a newbie and have been given the task of sorting a CSV file with IP range and location.
The format of the file is like this:
1.2.3.4,1.2.3.255,USA
2.3.4.0,2.3.4.25,USA
124.2.3.0,124.2.3.255,Argentina

I need to sort the file based on IP Address, country wise
Pls HELP !!!
  • Comment on How do I sort a file with IP Addresses and another text column

Replies are listed 'Best First'.
(tye)Re: How do I sort a file with IP Addresses and another text column
by tye (Sage) on Jun 14, 2001 at 09:56 UTC
    print # 5) Print the sorted lines. map { # 4) Transform lines back to original: my( $co, $ip, $e )= # 4a) Split out the 3 fields split /,/; $ip= join ".", map { # 4b) Remove zero padding from IP addr sprintf "%d", $_ } split /\./, $ip; "$ip,$e,$co\n" # 4c) Restore field order and newline } sort # 3) Sort the transformed data by char. map { # 2) Transform lines so default sort works: chomp; # 2a) Remove trailing newline my( $ip, $e, $co )= # 2b) Split out the 3 fields split /,/; $ip= join ".", map { # 2c) Pad IP addr w/ zeros so sorts OK sprintf "%03d", $_ } split /\./, $ip; "$co,$ip,$e" # 2d) Put fields in the order of sort } <>; # 1) Read in all of the lines.
            - tye (but my friends call me "Tye")
Re: How do I sort a file with IP Addresses and another text column
by bikeNomad (Priest) on Jun 14, 2001 at 09:42 UTC
    Well, rather than doing it for you, I'll try to point you to some information that you can use to do it one way (remember, TMTOWTDI):<bl>
  • You can split up your line on commas and/or periods using split
  • You can use Array Slices to make sub-arrays out of arrays
  • You can use pack to stick the dotted numbers together as 4-character binary strings, after splitting them.
  • You can use the Schwartzian Transform to process the data, sort it, and then transform it back to a printable format
  • You can use sprintf with the "%vd" format specifier to print a 4-character binary string as a dotted IP address (if you have Perl 5.6.0 or later)
  • </bl>

    I'm sure someone else will come up with something simpler; this is what occurred to me.

Re: How do I sort a file with IP Addresses and another text column
by Arguile (Hermit) on Jun 14, 2001 at 10:01 UTC

    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...