use Data::Dumper; open(FH,"x.txt") or die("Can't open the file: $!"); $/=EOF; my $var = ; my %hash=(); while ($var =~ /(same_text\s=\s\d),((\s\S+){1,2})/gi) { my ($junk,$ky)=split(/=/,$1); push(@{$hash{$ky}},$2); } map { print "$_ -- @{$hash{$_}} \n "; } keys %hash;