Need help with some iterative file processing

Gurus - Please help with this urgent situation.

I have the following problem to solve using a shell script:

  1. There are about 13 files named CONTAINER_1.lst, CONTAINER_2.lst, CONTAINER_3.lst .....CONTAINER_13.lst
  2. Each of these files contain about 8 lines (in most cases) containing container instance names like CONTAINER_X_InstanceY where X will correspond to the container in question (ranging from 1 thru 13) and Y is some random variable value
  3. For each of the 13 containers, there is a file named, CONTAINER_X_commands.sh where X corresponds to the container number. Each of the CONTAINER_X_commands.sh file contains about 3 to 5 commands with a fixed embedded string called "HELLOWORLD"
  4. For each container name, I would like to first read all the CONTAINER_X_InstanceY variables (one at a time), then repeat each of command in the CONTAINER_X_commands.sh file, N number of times where N is the number of lines in the CONTAINER_X.lst (equal to the number of container instances) Once the yanking/creating multiple copies of commands is done, I would like to replace each occurrence of the "HELLOWORD" string with CONTAINER_X_InstanceY container instance name variable

Here is the code I have written so far :
for container_list in `ls -1 CONTAINER*.lst | sort -u`
do
container_name=`echo $container_list | sed 's;\(.*\)\..*;\1;'`
cmd_suffix=_commands.sh
command_file=$container_name$cmd_suffix
for container_instance_name in `cat $container_list`
do
for command in `cat $command_file`
do
echo $command | sed 's/HELLOWORLD/$container_instance_name/g;' >> /tmp/all_commands_in_singlefile.lst
done
done
done

There are 3 problems in my output :

  1. It's printing each word in the commands.sh file on separate line. I would like to keep each command intact on it's own line without breaking each command word on separate line (I think this is an issue with my innermost for loop)
  2. the sed is actually replacing HELLOWORLD with the literal string "$container_instnace_name" instead of using the actual variable value
  3. I am also suspecting that the output file is containing lesser number of rows of commands, than I would actually anticipate. But this is yet to be substantiated because of newlines introduced in my output.

Any help will be greatly appreciated!
indi

Gurus,

First of all apologies for my verbose post.

I think I have solved issue# 1 and issue# 3 by changing the code in following manner :

#/bin/sh

for container_list in `ls -1 CONTAINER*.lst | sort -u`
do
container_name=`echo $container_list | sed 's;\(.*\)\..*;\1;'`
cmd_suffix=_commands.sh
command_file=$container_name$cmd_suffix
for container_instance_name in `cat $container_list`
do
cat $command_file | while read line
do
cmd_line=`echo $line | sed 's/HELLOWORLD/$container_instance_name/g;'`
`echo $cmd_line >> /tmp/all_commands_in_singlefile.sh
done
done
done
##

However my issue# 2 is still open. sed is still replacing HELLOWORLD with the literal string "$container_instnace_name" rather than the contents of the variable. Do I have to use some escape sequence to let sed know that it's supposed to use the value of the variable as against using the liternal string? It's kinda urgent, so any help will be really appreciated!

thanks,
indi