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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |