You could probably use bash to do this, but I like Python, and it seems to be rarely used to solve problems... Schmeh, here goes...
output = ""
words = 
replacementsFile = open("repfile")
for word in replacementsFile:
wordToReplace = re.compile("\$word\d")
htmlFile = open("htmlfile")
for line in htmlFile:
output += wordToReplace.sub(line,words.pop(0))
outputFile = open("outputFile","w")
Disclaimer: this hasn't been tested, but it should work *grins in trustworthy manner*
However, that's horrible and kludgey because your problem is. Is every line in the output file going to be in the same format? Why not just read in the first file, have the HTML coded into the programme and just output the lines? You need very little modification to the bash code you have already in order to do that - just a couple of "echo" statements. You seem to have unnecessary steps... Apologies in advance if I've actually just misunderstood you