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>
LVL 1
1 LinePerlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
#!/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;
0
1 LinePerlAuthor Commented:
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

0
ozoCommented:
#!/usr/local/bin/perl
use strict;
use warnings;

my @lines;
while (my $line = <>) {
    if ( $line !~ m(mgmt) && $line !~ m(BGP) ) {
      push @lines,join"\t",(split/\s+/,$line)[6,7];
    }  
}
$\="\n";
print for sort{ (pack"C*",split/\D+/,$a) cmp (pack"C*",split/\D+/,$b) }  @lines;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
1 LinePerlAuthor Commented:
Perfect, thanks Ozo
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.