in reply to Re: perl quicker than bash?
in thread perl quicker than bash?
output:use strict; use warnings; my ( %masked, @results ); while ( my $line = <STDIN> ) { my ($ip) = $line =~ / (\d+ \. \d+ \. \d+ \. \d+) /x or next; my $mask = pack 'C3', split /\./, $ip; if ( $masked{$mask} ) { if ( not $masked{$mask}{repeat} ) { $masked{$mask}{ip} =~ s{ \d+ \z }{0/24}x; $masked{$mask}{repeat} = 1; } } else { $masked{$mask} = { ip => $ip, }; push @results, $masked{$mask}; } } print $_->{ip}, "\n" for @results;
This way you also don't need to depend on the ordering of the source file (works with any order). As for dealing with comments and different cidrs I'll "leave it as an exercise for the reader" :)1.2.3.0/24 1.4.3.5 2.3.1.2 2.3.2.0/24
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: perl quicker than bash?
by Anonymous Monk on Jan 08, 2015 at 02:35 UTC | |
by Anonymous Monk on Jan 08, 2015 at 22:23 UTC | |
by Anonymous Monk on Jan 08, 2015 at 02:42 UTC | |
by TiffanyButterfly (Novice) on Jan 08, 2015 at 04:35 UTC | |
by TiffanyButterfly (Novice) on Jan 08, 2015 at 04:50 UTC | |
|
Re^3: perl quicker than bash?
by TiffanyButterfly (Novice) on Jan 07, 2015 at 19:56 UTC | |
by Anonymous Monk on Jan 07, 2015 at 21:19 UTC | |
by TiffanyButterfly (Novice) on Jan 07, 2015 at 23:13 UTC | |
by Anonymous Monk on Jan 08, 2015 at 02:29 UTC |