Need Help in rearranging the content of a file comma seperated

I have a file with the below content

a = test1
b =  test2
a = test3
b= test4
c = test6
b = test5
d = test7
d = test9

Need the output to be as follows

a = test1,test3
b = test2, test5
c = test6
d = test7, test9

Not able to figure out how to do it :frowning:

Why are there spaces in the output before test5 and test9 but no space before test3 ?

Why doesn't test4 appear in the output?

While we can deal with variations (e.g. varying count of spaces) in the input file, the variations (inconsistencies?) in your output can't be met unless you specify exactly where and when spaces should be inserted. Try:

awk '
        {gsub(/ /,"")
         T[$1]=T[$1] DL[$1] $2
         DL[$1]=","
        }
END     {for (t in T) print t " = " T[t]
        }
' FS="="  file
a = test1,test3
b = test2,test4,test5
c = test6
d = test7,test9

Hello iron_michael86,

Following may help you in same too.

awk -F" *= *" '{A[$1]=A[$1]?A[$1] OFS $NF:$NF} END{for(i in A){print i " = " A}}' OFS=,  Input_file

Output will be as follows.

a = test1,test3
b = test2,test4,test5
c = test6
d = test7,test9
 

Thanks,
R. Singh

perl -nlaF'/\s*=\s*/' -e 'push @{$seen{$F[0]}}, $F[1]; END{$"=q{,};for $k (sort keys %seen){print "$k = @{$seen{$k}}"}}' iron_michael86.file
a = test1,test3
b = test2,test4,test5
c = test6
d = test7,test9

or

perl -nlaF'/\s*=/' -e 'push @{$seen{$F[0]}}, $F[1]; END{$"=q{,};for $k (sort keys %seen){print "$k = @{$seen{$k}}"}}' iron_michael86.file 
a =  test1, test3
b =   test2, test4, test5
c =  test6
d =  test7, test9