in reply to Re^2: perl quicker than bash?
in thread perl quicker than bash?

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 ]; }

Replies are listed 'Best First'.
Re^4: perl quicker than bash?
by Anonymous Monk on Jan 08, 2015 at 22:23 UTC
    elsif ( $old_cidr > $new_cidr ) {
    should be
    elsif ( $new_cidr and $old_cidr > $new_cidr ) {
    come to think of it.
Re^4: perl quicker than bash?
by Anonymous Monk on Jan 08, 2015 at 02:42 UTC
    It preserves biggest found cidr
    s/biggest/smallest/ :)

      Awesome! Thanks very much for your hard work! :)

      I'll give it a go. This all helps me to learn the syntax.

      Yes - it's hard to tell one Anon Monk from the next - you all look alike to me! (haha)

      :)

        lol... that thing is great!

        I ran a diff with the output from the bash script and your perl script and both outputs are identical. Just timed it too - on my original list of (now) 1,480 IP addresses, your perl script took ~150ms to run... well done! :)