use strict; use warnings; use Data::Dumper; my %hash; open ( FH, "powermt display dev=all|");# data stream comes from here $/ = ''; while () { my ($id) = ( $_ =~ /Logical device ID=(\w+)/ ); push (@{$hash{$id}}, $1) if /(c\d+t\d+d\d+)/; } print Dumper (\%hash);