#!/usr/bin/perl
while(<>){ @udp=@tcp=(), $udp=$tcp=0, $Index=$OS="";
$$2=push @$2, $1 while(m#([0-9]+)/[a-z]+/(udp|tcp)/////[,]*#g);
$$1=$2 while(m#([^ \n:]+): ([^ \n]+)#g);
$OS=$1 if(m#OS: ([^\n]+)#); #} complete code to parse nmap logfile 

# Usage:        cat nmap_machine_output_file | perl this_program.pl
#       OR      perl this_program.pl < nmap_machine_output_file
#       OR      perl this_program.pl nmap_machine_output_file
#
# provides      @udp, @tcp (arrays of ports)
#               $udp, $tcp (number of ports for each)
#               $Host      (ip address in string format)
#               $Index     (Sequence Index, if avail)
#               $OS        (OS name, if avail)

##########################################################
#  Put your code here
##########################################################

# Example/demo code

print "IP: $Host";
print " (Seq: $Index OS: $OS)" if ($OS ne "");
print "\nTotal TCP ports: $tcp\n";
print join " ", @tcp;
print "\nTotal UDP ports: $udp\n";
print join " ", @udp;
print "\n\n";

### End of your code #####################################
}


