use warnings; use strict; my %hash; while(){ chomp; my ($dns)=grep $_=>split/^.+?:/,$_; my ($ip,$dns1)=split/,/,$dns; $hash{$dns1}=$ip; } print $hash{$_},$/ foreach keys %hash; __DATA__ host1 dns:192.168.243.30,asdf host2 dns:192.168.243.1,qwert