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.$ perl squeeze_ips.pl -i input.list -o output.list
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |