If you must do it in Perl use the Schwartzian Transform for efficiency. This avoids finding the 4th octet more than once for each line and is thus fast(er) than it might otherwise be.
my @unsorted = <DATA>; my @sorted = map{ $_->[0] } sort{ $a->[1] <=> $b->[1] } map { [$_, get_fourth($_) ] } @unsorted; sub get_fourth { local $_ = shift; /\d+\.\d+\.\d+\.(\d+)/; return $1; } print "@sorted"; __DATA__ 10.194.196.55 heckyll 10.194.196.57 txwl013w 10.194.196.61 krios 10.194.196.62 stasis # dial up server 10.194.196.63 nnocdts01 # sparc 5 in wan rm 10.194.196.65 libra 10.194.196.66 legato-school 10.194.196.67 txwlxdbs nnocs002a 10.194.196.46 chqnocp01 # Laser 4MV by Joe Blow 10.194.196.47 chqnocp02 10.194.196.49 nnocrpt01 10.194.196.64 saturn 10.194.196.50 nnocgw001 10.194.196.52 nnocs002 10.194.196.53 wslkms02 wslknms0 10.194.196.54 nnocws001
Changed order to ascending numrical order as per spec (I think)
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
In reply to Re: sorting /etc/hosts
by tachyon
in thread sorting /etc/hosts
by tmbailey123
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |