#!/usr/bin/perl -w use strict; my $infile = './portscan2.txt'; my ($ip, @fields, @cname, @ports, @banners); my @field_names = qw(cname port banner); my %data; # Change the input record separator local $/ = "* +"; open INFILE, "$infile" or die "Can't open $infile: $!\n"; while (){ next unless /\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/; $ip = $1; push (@cname, $1) if /\b$ip\b\s{2,}(\w[^\s]*|\[Unknown\])\s{2}/; push (@ports, $1) if /((\d{1,5}?)\s{2}(\w[^\n]*))\s{2}/; push (@banners, $1) if /\Q|___\E\s+([^"]*)\.{1,3}\s+\n/; my $fields = [@cname, @ports, @banners]; @{$data{$ip}}{@field_names} = [$fields]; } foreach my $ip (keys %data) { print "IP = $ip, Computer Name = $data{$ip}{cname}->[0][0]\n"; } close INFILE;