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.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.