use strict; use warnings; my $file = 'input.txt'; my (%hash, @ips, @alerts); open (FILE, "$file") or die "Can't open $file\n"; local $/ = '-------------------------------------------------'; while (){ $hash{$2}{$1} = 0 if (/(Host: \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*(Service:.*Severity:.*)/ms); } foreach my $key (keys %hash) { $hash{$key} = [keys %{$hash{$key}}]; } my ($key, $values); while (($key, $values) = each %hash) { print "$key --> ", join('|', @$values), "\n"; }