use Socket qw( inet_aton ); use List::MoreUtils qw( apply ); my $file_name = "ipsvisiting.txt"; open (my $fh, '<', $file_name) or die("Unable to open file \"$file_name\": $!\n"); my @sorted = map { substr($_, 4) } sort map { inet_aton($_) . $_ } apply { chomp } <$fh>; print "$_\n" foreach @sorted;