This little program has all the elements needed. It will acculumate IP ranges by the id name and expand the range to include the min and max ip addresses. It does not check for holes between ranges in the same id which if the input data is sane is not needed.

#!/usr/bin/perl use strict; my($start, $end, $id, %range); while(<>) { chop; ($start, $end, $id)=split(','); $start=pack("C4", (split('\.',$start))); $end=pack("C4", (split('\.',$end))); if($range{$id}) { my($os, $oe)=@{$range{$id}}; $os=$start if($start lt $os); $oe=$end if($end gt $oe); @{$range{$id}}=($os, $oe); } else { @{$range{$id}}=($start, $end); } } foreach my $r ( sort keys %range ) { printf "%vd,%vd,%s\n", @{$range{$r}},$r; }

The addresses and apparently v strings in general are strings so you have to use string comparison operators on them. But, due to the conversion, 10.20.30.40 is smaller than 10.20.30.100 which as normal strings would not be true. the %vd prints out dotted decimal %vb prints out a bit string %vX prints out an IPv6 address.


In reply to Re: IP Address consolidation by dga
in thread IP Address consolidation by yasysad

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.