concatenate consecutive field values

Hi,

I have a file like this

A  Bob
A  Sam
A  John
B  David
C  Paul
C  Sandra

If the consecutive field values in column one is same, then concatenate the corresponding strings.

So, I need an output like this,

A Bob_Sam_John
B David
C Paul_Sandra

I usually work with excel but couldn't make it. Any ideas how I can achieve this with AWK?

thanks in advance.

Try:

awk 'p!=$1{if(NR>1)print s;s=$0;p=$1;next}{s=s"_"$2}END{print s}' infile

thanks a lot - its working perfectly.:b::slight_smile:

awk '{a[$1]=a[$1]"_"$2}END{for (i in a) {sub(/^_/,"",a);print i,a}}' urfile