#! /usr/bin/perl use warnings; use strict; my %source; my $host; while (<>) { if (/^ok: \[(.*)\] => \{/) { $host = $1; } elsif (/^\s+"(\S+)\s+nfs\s/) { push @{ $source{$1} }, $host; # Or maybe # push @{ $source{$host} }, $1; } } use Data::Dumper; print Dumper \%source;