filtering for identical field labels



I'm having trouble figuring how to do the following.

I've a file like this:

***record***
TAG1 A
TAG2 B
irrelevant------------------
TAG2 C
TAG3 D
TAG4 E
#
***record***
TAG1 f
TAG2 g
TAG2 h
irrelevant------------------
TAG3 i
TAG4 j
#

So the record delimiter is actually #\***record***, and the field delimiter
is \n. The records are of variable lengths, and may contain multiple
identical field names, which I show above with the repetitions of tag2.

I am trying to get to a csv file from this, and can do most of it, but the
thing I am stuck on is how to deal with the multiple field names.

What I need to get to from the above is a file that looks like this:

A TAB B C TAB D TAB E
f TAB g h TAB i TAB j

The thing I can't figure out is how to check for two or more identical field
names in a row in the input file, and then put the contents into the same
field in the csv file.

Regards, Al
.