Created
February 27, 2022 14:33
-
-
Save dpavlin/9e9e66eca8b8bfc26af8f6277445c172 to your computer and use it in GitHub Desktop.
sort text output with ip addresses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use warnings; | |
use strict; | |
use Socket qw( inet_aton ); | |
use Getopt::Long; | |
my $col = 1; | |
my $del = '\s+'; | |
GetOptions( | |
# sort | |
'field-separator|t=s' => \$del, | |
'key|k=i' => \$col, | |
# cut | |
'delimiter|d=s' => \$del, | |
'fields|f=i' => \$col, | |
); | |
my @lines; | |
my @sort; | |
while(<>) { | |
chomp; | |
push @lines, $_; | |
my @f = split(/$del/, $_); | |
my $ip = $f[ $col - 1 ]; | |
push @sort, inet_aton($ip) . $#lines; | |
} | |
my @sorted = map { | |
substr($_,4) | |
} sort @sort; | |
foreach my $i ( @sorted ) { | |
print $lines[$i], "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment