Cheap perl script. I't should sort on an IP address, but should be extendible. I can't belive it fits two lines.
#!perl $s='(\d+\.\d+\.\d+\.\d+)'; while(<>) { $d[$i++]=$_;} print sort {my($c,$d); $a=~/$s/; $c=$1; $b=~/$s/; $d=$1; $c cmp $d;} @ +d;

Replies are listed 'Best First'.
Re: Sort in IP in 2 lines.
by I0 (Priest) on Aug 07, 2001 at 10:17 UTC
    print sort{($a=~/(\d+(\.\d+){3})/)[0]cmp($b=~/(\d+(\.\d+){3})/)[0]}<>;

      Uh... that doesn't sort IPs in the way you might expect IPs to be sorted...

      perl -le 'print for sort{($a=~/(\d+(\.\d+){3})/)[0]cmp($b=~/(\d+(\.\d+ +){3})/)[0]} qw(1.2.3.4 1.12.3.4);' 1.12.3.4 1.2.3.4

      In fact, it doesn't do much of anything except a run-of-the-mill asciibetical sort of lines based on the first portion of them that looks somewhat like an IP.

      I can't believe that this has sat around for almost two years without reply... I hope no one out there has actually used it!

      -sauoq
      "My two cents aren't worth a dime.";
      
Re: Sort in IP in 2 lines.
by sauoq (Abbot) on May 30, 2003 at 21:00 UTC

    See my reply to IO. It applies to this one as well...

    sauoq sighs...

    -sauoq
    "My two cents aren't worth a dime.";
    
      In fact it's even worse - it's so broken I don't even know where to begin. The assignments from $1 don't depend on the purportedly associated pattern's success, so if there's nothing looking like an IP in $b both lines will be considered equivalent even though one of them doesn't even have an IP. Worse yet, if $a doesn't match, it will compare $b against a random previous IP - that is, if $b matches, otherwise it will accidentally do the right thing (two lines both without any IP-like patterns will be considered equal). And so on..

      Makeshifts last the longest.

Re: Sort in IP in 2 lines (program repair shop)
by Aristotle (Chancellor) on May 30, 2003 at 21:53 UTC
    A regex to match (optionally zero padded, decimal) dotted-quad IPs is fairly involved.
    #!/usr/bin/perl -w use strict; my $octet = q[0?\d\d?|1\d\d?|2[0-4]\d?|25[0-5]|2[6-9]]; # MUST BE CONS +TRAINED my $ip = q[(?<!\d\.)] . join(q[\.], ( "($octet)" ) x 4) . q[(?!\.\d)]; my $rx = qr/$ip/;
    Even that isn't perfect(!), but it's much closer. To sort by IP, you should capture the octets and feed them to pack "C4", yielding a string that can be used for ASCIIbetical sorting. This is a basically a task that naturally lends itself to a GRT:
    my @line; while(<>) { push @line, pack("C5", (@octet = /$rx/ ? 1, @octet : (0) x 5 ) ) . + $_; } print map substr($_, 5), sort @line;
    Note I'm using C5 here to be able to distinguish lines with no IP at all from those with 0.0.0.0.

    Makeshifts last the longest.