Merge rows into one if first 2 columns match

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Merge rows into one if first 2 columns match

susmitha
Hi,

I wanted to merge the content and below is input and required output info.

Input:
/hello,a,r
/hello,a,L
/hello,a,X
/hi,b,v
/hi,b,c

O/p:

/hello,a,r:L:X
/hi,v,:v:c

please help me to get it.
Reply | Threaded
Open this post in threaded view
|

Re: Merge rows into one if first 2 columns match

Jean-Paul
Hi Susmitha,

The code awk script works fine for that case.

This is a classical question on report generation .

#awk -F"," '
$1 $2 != Ref { if ( Ref > "" ) print Output
                                Ref = $1 $2  
                                Output  = $1 "," $2 "," $3
                                next
                                }
$1 $2 == Ref { Output = Output "," $3}
END {print Output }' file.txt