Okay, that was a different anonymonk explaining pack and recommending Modern Perl, but now it's me again :) I had nothing better to do and decided to actually write this thing for you, seeing your enthusiasm (to give you a taste of Perl). Here it goes. The program accepts command line options '-i' and '-o', meaning input and output. Otherwise it operates on stdin and stdout. Usage:
$ perl squeeze_ips.pl -i input.list -o output.list
It preserves biggest found cidr and the first encountered comment (changing it to the last found comment is easy enough). Processing a file of one million ips takes about 15 seconds on my laptop.
use strict; use warnings; use Getopt::Long; my ( $in, $out ) = get_in_out(); my $ips = process_file($in); for (@$ips) { my $ip = $_->{ip}; if ( $_->{cidr} ) { $ip .= '/' . $_->{cidr}; } print $out $ip, $_->{comment}, "\n"; } exit 0; sub process_file { my ($in) = @_; my ( %masked, @results ); while ( my $line = <$in> ) { my $ip = get_ip($line) or next; my $old_ip = $masked{ $ip->{mask} }; if ($old_ip) { update_ip( $old_ip, $ip ); } else { $masked{ $ip->{mask} } = $ip; push @results, $ip; } } return \@results; } sub get_ip { my ( $ip, $cidr, $comment ) = $_[0] =~ m{ ( \d+ \. \d+ \. \d+ \. \d+ #ip ) #mandatory (?: / (\d+) #cidr )? #optional ( \s* /\* .* \*/ #comment )? #optional }x or return undef; my $mask = pack 'C3', split /\./, $ip; $cidr ||= 0; # if perl >= 5.010 better use //= $comment ||= ''; # ditto return { ip => $ip, mask => $mask, cidr => $cidr, comment => $comment, }; } sub update_ip { my ( $old_ip, $new_ip ) = @_; my ( $old_cidr, $new_cidr ) = ( $old_ip->{cidr}, $new_ip->{cidr} ); if ( $old_cidr == 0 ) { $old_ip->{ip} =~ s/ \d+ \z /0/x; $old_ip->{cidr} = $new_cidr ? $new_cidr : 24; } elsif ( $old_cidr > $new_cidr ) { $old_ip->{cidr} = $new_cidr; } } sub get_in_out { GetOptions( 'in=s' => \my $in, 'out=s' => \my $out ); my ( $in_fh, $out_fh ); if ( not defined $in ) { $in_fh = \*STDIN; } else { open $in_fh, '<', $in or die "Can't open $in for reading: $!\n"; } if ( not defined $out ) { $out_fh = \*STDOUT; } else { open $out_fh, '>', $out or die "Can't open $out for writing $!\n"; } return wantarray ? ( $in_fh, $out_fh ) : [ $in_fh, $out_fh ]; }

In reply to Re^3: perl quicker than bash? by Anonymous Monk
in thread perl quicker than bash? by TiffanyButterfly

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.