Awk Multiline Record Combine?

I'm trying to use Awk to get the id and name fields ($1 and $2) of file1 combined with their corresponding multiline records in file2 that are separated by blank line. Both files are ordered so that the first line of file1 corresponds to the first set of multiline records in file2 and so on.

file1.txt

23315:MR FANOVER:BR:H:1994
45322:MR DEWEY:B:F:1996
23316:MR DREAMMAKER:BLK:X:1996
45323:MR MAN:BR:L:1997

file2.txt

99-01-14:BML:10:FT
99-01-31:BML:37:GD
99-06-18:MAY:73:FT
99-06-30:BML:71:SL

99-01-15:WDB:17:FT
99-01-25:MOHO:28:GD

99-07-2:BR:75:SY

99-12-6:NFLDO:32:SL
99-12-13:NFLD:39:FT
99-12-19:NFLD:40:FT

Desired Output:

23315:MR FANOVER:99-01-14:BML:10:FT
23315:MR FANOVER:99-01-31:BML:37:GD
23315:MR FANOVER:99-06-18:MAY:73:FT
23315:MR FANOVER:99-06-30:BML:71:SL

45322:MR DEWEY:99-01-15:WDB:17:FT
45322:MR DEWEY:99-01-25:MOHO:28:GD

23316:MR DREAMMAKER:99-07-2:BR:75:SY

45323:MR MAN:99-12-6:NFLDO:32:SL
45323:MR MAN:99-12-13:NFLDO:39:FT
45323:MR MAN:99-12-19:NFLDO:40:FT

Can anybody offer a solution?

Use nawk or /usr/xpg47bin/awk on Solaris:

awk -F: 'NR == FNR { _[NR] = $1 FS $2; next }
!$1 ? ++c : $1 = _[c] FS $1
' OFS=: file1 c=1 file2 

Thanks radoulov that did the trick! You're a life-saver or maybe a database-saver in this case!

Glad it worked!
Actually, it could be golfed :slight_smile:

awk -F: '
  NR==FNR{_[NR]=$1FS$2;next}$1?$1=_[c]FS$1:++c
  ' OFS=: file1 c=1 file2

perl:

$n=0;
open FH,"<b";
while(<FH>){
	$_=~tr/\n//d;
	if(length>0){
		if($arr[$n] eq ""){
			$arr[$n]=$_;
		}
		else{
			$arr[$n]=sprintf("%s!%s",$arr[$n],$_);
		}
	}
	else{
		$n++;
	}
}
close FH;
open FH,"<a";
while (<FH>){
	$temp=$arr[$.-1];
	@temp=split("!",$temp);
	@res=split(":",$_);
	for($i=0;$i<=$#temp;$i++){
		print "$res[0]:$res[1]:$temp[$i]\n";
	}
	print "\n";
}
close FH;

awk:

nawk -F":" 'BEGIN{c=1}
{
if(NR==FNR){
        _[NR]=$1FS$2
}
else{
        if(length($0)>0)
                print _[c]":"$0
        else
                c++
}
}
' a b