Doing multiple tasks in a loop.

I am in the process of updating a folder of hundreds of recipe html files. I've already managed to modify a number of things in each file but I have run into something that's beyond my ability.

I have a text file that I need to insert the contents into the html at a specific point. It creates several link buttons near the top of all the html pages. With the help of several people in this forum, I have a function that does this. I've even modified it for other modifications I have made to the html files and it works well. I've run into a roadblock though. The html recipe files are for a number of food categories and what html I insert for the function I am working on now is dependent upon what category the recipe is in. Unfortunately all these files are in one folder and are not named in a way that they can be sorted into categories. The only way to determine the category is to parse the html of each file.

Here is the function that loops through all the html files and inserts the text file contents in the correct position in each:

for file in *.htm ; do
while read line
do
grep -q '<div class="recipe"><img' <<<$line && cat cakesandcheesecakes.txt >> "$file".tmp
  echo $line >> "$file".tmp
done < "$file"
done

However, as I have said, the text to insert is dependent on the recipe category. I have text files for each category.

The only way to determine the category of each file is to parse the html. Here are examples from several different categories. The overall format is the same for all 800 files.

<span class='label'>Category:</span> Cakes and Cheesecakes</p>

<span class='label'>Category:</span> Cookies</p>

<span class='label'>Category:</span> Main Dishes</p>

Once I determine what the category of the current file being processed is I am going to have to call a modified version of the function I have in the first code box that inserts the correct text.

Cakes and Cheesecakes = cakesandcheesecakes.txt
Cookies = cookies.txt
Main Dishes = maindishes.txt
etc., etc.,etc.
(There are 17 categories overall)

I am perplexed regarding how I can loop through each html file, determine the category and insert the appropriate text according to the category.

Any guidance regarding how I need to approach this?