merge rows based on a common column

Hi guys,

Please guide me if you have a solution to this problem. I have tried paste -s but it's not giving the desired output.

I have a file with the following content-

A123 box1
B345 bat2
C431 my_id
A123 service
C431 box1
A123 my_id

I need two different outputs-

OUTPUT1
A123 box1 service my_id
B345 bat2
C431 my_id box1

OUTPUT2
box1 A123 C431
bat2 B345
my_id C431 A123
service A123

Your help is highly appretiated. Thanks in advance. :slight_smile:

Try this:

awk '{ x[$1]=x[$1] " " $2; y[$2]=y[$2] " " $1; } 
END { 
   for (k in x) print k,x[k] >"OUTPUT1";  
   for (k in y) print k,y[k] >"OUTPUT2"; 
}' INPUT

Thanks Otheus!

It is working perfect. I'll be grateful if u can explan the code a bit.

Thanks again. :slight_smile:


awk '
{ 
   x[$1]=x[$1] " " $2;   # Append 2nd field to contents of x[$1] (followed by a space)
   y[$2]=y[$2] " " $1;  # Append 1st field to contents of y[$1] (followed by a space)
} 
# Now: The array "x" contains all the lines keyed by the first field. 
#         The array "y" contains all the lines keyed by the second field.

END { 
   for (k in x) print k,x[k] >"OUTPUT1";  
   for (k in y) print k,y[k] >"OUTPUT2"; 
}' INPUT
1 Like

Thanks a lot Otheus.

awk '{
arr[$1]=sprintf("%s %s",arr[$1],$2)
brr[$2]=sprintf("%s %s",brr[$2],$1)
}
END{
for(i in arr)
        print i""arr
print "-------------------"
for(j in brr)
        print j""brr[j]
}' filename

Thanks summer_cherry. :slight_smile: