my %unitMap; my $cmd = "hpacucli ctrl slot=0 pd all show";; open my $fd, "$cmd|" or return \%unitMap; my $printit = 1; while (my $row = <$fd>) { next if $row =~ /^$|Smart/; $printit = 0 if $row =~ /^\s*unassigned$/; $printit = 1 if $row =~ /^\s*array .$/; next unless $printit; # moved processing to here; no need to manipulate lines you're not going to print anyway $row =~ s/^\s+//; $row =~ s/[,|)|(]//g; print "$row"; } close $fd;