my $data = do { local $/; <> }; # or however you load it. my( %device, %system ); for ( split /\n(?=\S)/, $data ) { if ( /^device for (.*?): (\S+)/ ) { $device{$1} = $2; } elsif ( /^system for (.*?): (\S+)/ ) { $system{$1} = $2; } elsif ( /^printer (\S+)/ ) { my $p = $1; my %printer = ( name => $p, locus => $device{$p} ? "Network" : $system{$p} ? "local" : "Remote", description => (/Description: (.*)/)[0], up => "Up", zero => 0, ); if ( $system{$p} ) { $printer{'interface'} = $system{$p}; $printer{'remote'} = "N/A"; } else { ( $printer{'interface'} ) = /Interface: (.*)/; $printer{'interface'} ||= ''; # in case it's absent $printer{'interface'} =~ s,.*/,,; # strip path info $printer{'remote'} = "no"; } print join('|', @printer{qw( name locus interface description up zero remote )}), "\n"; } }