in reply to Using unix commands in perl?

Hi ,
The code i have writtenis as follows :
open(file_info,$file) or die "Can't open $file "; while (<file_info>) { my ( $src_ip, $src_port ) = /IP\s+(\d+(?:\.\d+){3})\.(\d+)/; my ( $dst_ip, $dst_port ) = />\s+(\d+(?:\.\d+){3})\.(\d+)/; print "$src_ip , $src_port , $dst_ip , $dst_port \n" ; } close(file_info);
This displays the source ip, port, destination ip and port
i need to sort these records and take the uniq ones and display them

Himi

Replies are listed 'Best First'.
Re^2: Using unix commands in perl?
by erroneousBollock (Curate) on Oct 06, 2007 at 05:16 UTC
    Update: actually the following not going to be quite correct... need to pack the IP addresses as integers for the sort... so a more generalised (schwartzian) transform is probably better. See my next post in this thread.

    use warnings; use strict; open(file_info,$file) or die "Can't open $file "; my @records; while (<file_info>) { my ( $src_ip, $src_port ) = /IP\s+(\d+(?:\.\d+){3})\.(\d+)/; my ( $dst_ip, $dst_port ) = />\s+(\d+(?:\.\d+){3})\.(\d+)/; push @records, [$src_ip , $src_port , $dst_ip , $dst_port]; } close(file_info); my %seen; my @result = sort { $a->[0] != $b->[0] ? $a->[0] <=> $b->[0] : $a->[1] != $b->[1] ? $a->[1] <=> $b->[1] : $a->[2] != $b->[2] ? $a->[2] <=> $b->[2] : $a->[3] <=> $b->[3] } map { $seen{(join(',',@$_))}++ ? () : $_ } @records; print join(' ',@$_)."\n" for @result;

    -David