Skip to content

Instantly share code, notes, and snippets.

@dpavlin
Created February 27, 2022 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpavlin/9e9e66eca8b8bfc26af8f6277445c172 to your computer and use it in GitHub Desktop.
Save dpavlin/9e9e66eca8b8bfc26af8f6277445c172 to your computer and use it in GitHub Desktop.
sort text output with ip addresses
#!/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