Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting an array of IPs that also has other data.

Status
Not open for further replies.
Jun 3, 2007
84
0
0
US
Hello everyone I was hoping someone could please point me in the right direction with the problem I am having/trying to solve. I currently have an array which gets populated with data as shown below which has date time ips ports etc, similar info you would find in a log file.

2008-08-01 10.10.10.1 12373 2.2.2.2 80 hxxp://2.2.2.2 200 OK
2008-07-26 10.168.1.1 11122 3.3.3.3 80 hxxp://3.3.3.3 200 OK

Now what I am trying to do is sort/order by the source IP address which in this case would be 10.10.10.1 and 10.168.1.1 In the past I have used

the following to sort and array of IP's but not sure how to go about it when there are other fields in the array.


Code:
@ip (this would be a list of IP addresses);
use Sort::Key::IPv4 qw(ipv4sort);
my @sorted = ipv4sort @ip;

How would I got about sorting this data based on the IP address. FYI the IP address is always at the same location in the logs.

thanks for the help in advance.
 
You can definitely clean up the sorting a bit by using a custom sort function, but this should work for you.

Code:
my @lines = ('2008-07-26 10.168.1.1 11122 3.3.3.3 80 hxxp://3.3.3.3 200 OK',
			 '2008-07-15 10.5.2.1 11111 4.4.4.4 80 hxxp://4.4.4.4 200 OK',
			 '2008-08-01 10.10.10.1 12373 2.2.2.2 80 hxxp://2.2.2.2 200 OK');
			 
my @sorted = map {$_->[0]} sort {$a->[1] <=> $b->[1] || 
			 $a->[2] <=> $b->[2] || $a->[3] <=> $b->[3] ||
			 $a->[4] <=> $b->[4]} map {
			 [$_, split(/\./, (split /\s+/, $_)[1])]} @lines;

print "$_\n" for @sorted;
Take a look at this article for more information on using a Schwartzian Transform.
 
rharsh,

thanks for the reply, I was trying to follow what you did and got lost. If possible can you explain what you are doing each step of the way. I follow the splits that you did but not sure what you are doing with each of the
mapping and why. $a->[1] <=> $b->[1]

thanks for the help
 
Did you read through the information in the link I posted?

There's also a great FAQ written by KevinADC that explains what's happening. Take a peek at faq219-6545.

What I posted is slightly more complicated because of the two splits and the array slice. So if you read through the information in those two resources and still have questions, post something back here and I will be glad to help.
 
learingperl01, if I understand correctly your problem, you simply need to create a hash table, where each IP address is the key, and the corresponding log line is the value. Something like
Code:
my%lines=(
'10.168.1.1','2008-07-26 10.168.1.1 11122 3.3.3.3 80 hxxp://3.3.3.3 200 OK',
'10.5.2.1','2008-07-15 10.5.2.1 11111 4.4.4.4 80 hxxp://4.4.4.4 200 OK',
'10.10.10.1','2008-08-01 10.10.10.1 12373 2.2.2.2 80 hxxp://2.2.2.2 200 OK'
);
Then you just sort the hash keys outputting the corresponding hash values.
I suppose you know how to extract the IP address, otherwise come back.


Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top