Input file
steve,apples
steve,oranges
john,pears
john,oranges
mary,bananas
steve,plums
mary,nactarines
I want to get output like this:
steve:apples,oranges,plums
john:pears,oranges
mary:bananas,nectarines
Here is the one liner I have been trying to get to work:
awk -F, '{if(a[$1])a[$1]=a[$1]","$2; else a[$1]=$2;}END{for (i in a)print i ":" a[i];}' OFS=, inputfile
The output it gives is
,orangesrs
,plumsesples
,nactariness
It would appear that the string concatenation a[$1]=a[$1]","$2 is resulting in the original value of the array element to be overwritten to some degree. How can I carry out this concatenation correctly?
Incidentally, I get the same results on Centos, and Mac OSX.