#!/usr/bin/perl my %hash; while(){ chomp; my @line = split /,/; my $first = shift @line; push( @{$hash{$first}}, @line ); } foreach( sort keys %hash ){ print "$_\t".join(' ', sort @{$hash{$_}})."\n"; } __DATA__ text1,text-a,text-w text2,text-b,text-y text3,text-x,text-t text1,text-d,text-n text3,text-f,text-z text3,text-e,text-w text10,word,other 1 text10,aword,nother 1 text11,cword,tother 1 text11,zword,bother 1 __END__ prints: text1 text-a text-d text-n text-w text10 aword nother 1 other 1 word text11 bother 1 cword tother 1 zword text2 text-b text-y text3 text-e text-f text-t text-w text-x text-z