#!/usr/bin/perl use strict; use warnings; my %keys; while(my $line=) { $keys{$1}++ if($line=~/,(.+?)$/); } while (my ($key,$val)= each(%keys)) { print "$key = $val\n"; } __DATA__ Message = NPU$xy123/08Aug12344,ABS Message = NPU$xy123/08Aug12344,ASR Message = NPU$xy123/08Aug12344,ASR Message = NPU$xy123/08Aug12344,ABS09 Message = NPU$xy123/08Aug12344,ACT12 Message = NPU$xy123/08Aug12344,ABS Message = NPU$xy123/08Aug12344,ASR/88 Message = NPU$xy123/08Aug12344,ANTI Message = NPU$xy123/08Aug12344,ANTI2