Link to home
Start Free TrialLog in
Avatar of 1 LinePerl
1 LinePerl

asked on

Perl Sort Question

I have a simple script that parses a file and pulls the data from column6 and column7 (which is an IP address and subnet mask) I am trying to sort the list of IPs but it is not working as expecting. I think I probably have to sort prior to joining the columns but not sure how to do that.

#!/usr/local/bin/perl
use strict;
use warnings;

my @lines;

while (my $line = <DATA>) {
    my $string = $line;
    my @ipnum = split(/\s+/,$string); 
$string = join(" ",@ipnum);
print for sort{ $a <=> $b } "$ipnum[6]\t$ipnum[7]\n"; 
  }  

Open in new window





<DATA>
configure interface cable-mac 1.10 ip address 10.203.157.1 255.255.255.0 secondary
configure interface cable-mac 1.10 ip address 10.237.208.1 255.255.240.0 secondary
configure interface cable-mac 1.10 ip address 10.237.224.1 255.255.240.0 secondary
configure interface cable-mac 1.10 ip address 27.9.124.1 255.255.254.0 secondary
configure interface cable-mac 1.10 ip address 123.101.36.1 255.255.252.0 secondary
configure interface cable-mac 1.10 ip address 48.190.0.1 255.255.248.0 secondary
configure interface cable-mac 1.10 ip address 48.191.104.1 255.255.248.0 secondary
configure interface cable-mac 1.10 ip address 77.80.192.1 255.255.240.0 secondary
configure interface cable-mac 1.10 ip address 77.80.208.1 255.255.240.0 secondary
configure interface cable-mac 1.30 ip address 10.237.190.1 255.255.255.0
configure interface cable-mac 1.35 ip address 10.203.4.1 255.255.254.0 secondary
configure interface cable-mac 1.35 ip address 10.203.192.1 255.255.248.0 secondary
</DATA>

<RESULTS>
10.203.157.1 255.255.255.0
10.237.208.1 255.255.240.0
10.237.224.1 255.255.240.0
27.9.124.1 255.255.254.0
123.101.36.1 255.255.252.0
48.190.0.1 255.255.248.0
48.191.104.1 255.255.248.0
77.80.192.1 255.255.240.0
77.80.208.1 255.255.240.0
10.237.190.1 255.255.255.0
10.203.4.1 255.255.254.0
10.203.192.1 255.255.248.0
</RESULTS>
Avatar of ozo
ozo
Flag of United States of America image

#!/usr/local/bin/perl
use strict;
use warnings;

my @lines;

while ( <DATA> ){
    my @ipnum = split;
   push @lines,"$ipnum[6]\t$ipnum[7]\n";
}  
print for sort{ $a <=> $b }  @lines;  # or  print for sort{ (pack"C*",split/\D+/,$a) cmp (pack"C*",split/\D+/,$b) }  @lines;
Avatar of 1 LinePerl
1 LinePerl

ASKER

sorry ozo, in an attempt to make my script cleaner for asking the original question, omitted an "if" statement with some regex I use to pull just the data I need, but I can't seem to apply your answer to my full code and make it work. Here is my full script with the "if" regex line included. Can you please adapt your answer to this code?

#!/usr/local/bin/perl
use strict;
use warnings;

my @lines;

while (my $line = <>) {
    if ( $line !~ m(mgmt) && $line !~ m(BGP) ) {
    my $string = $line;
    my @ipnum = split(/\s+/,$string); 
$string = join(" ",@ipnum);
print "$ipnum[6]\t$ipnum[7]\n"; 
  }  
  }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect, thanks Ozo