in reply to removing a column
It is a good idea to check for the success of your opens and closes and to use the three argument form for open with lexical filehandles. You should also use single quotes if you don't need interpolation.
You need to take care when sorting IPs as a simple lexical sort will give odd results.my $hostfile = '/tmp/jn/hosts'; open my $hostFH, '<', $hostfile or die "open: < $hostfile: $!\n";
$ perl -le ' > @IPs = qw{ 22.32.87.12 56.67.38.61 101.12.34.54 }; > print for sort @IPs;' 101.12.34.54 22.32.87.12 56.67.38.61 $
I use sprintf here to form each quad into a fixed width string with leading zeros, making a 12-digit string that can be sorted lexically.
use strict; use warnings; my %seen; my @sortedUniques = map { ( split m{\s+}, $_->[ 0 ] )[ 0 ] } sort { $a->[ 1 ] cmp $b->[ 1 ] } grep { ! $seen{ $_->[ 1 ] } ++ } map { m{^(\d+)\.(\d+)\.(\d+)\.(\d+)\t} ? [ $_, sprintf q{%03d%03d%03d%03d}, $1, $2, $3, $4 ] : () } <DATA>; print qq{$_\n} for @sortedUniques; __END__ # hosts file 10.31.17.65 fw2 192.168.1.78 hosta 192.168.1.21 hostb 10.31.17.65 fw2dup 10.23.212.6 hostc 192.168.100.254 fw1 192.168.1.21 hostbdup
The output.
10.23.212.6 10.31.17.65 192.168.1.21 192.168.1.78 192.168.100.254
I hope this is helpful.
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: removing a column
by poolpi (Hermit) on Jun 18, 2009 at 09:07 UTC | |
by jn64024 (Initiate) on Jun 18, 2009 at 14:26 UTC |