title: SED in bash substitution with a variable, the variable being the content of another file stored in an array
body: <p>Basically a have a "template" file I want to fill with the content of some other auxiliary files which are, basically, a matrix of numbers and letters. I make other substitutions, but this one is what's giving trouble. </p>
  
  <p>In the template file I've put the "%label" at the place I want to do the substitution with the auxiliary file. Then I use this script to do the substitution:</p>
  
  <pre><code>for i in $(cat file_list.txt); do
      array[$i]=$(&lt;file_$i.txt)
  done
  
  for i in $(cat file_list.txt); do
      sed -e "s/%label/${array[$i]}/g"&lt; template.txt &gt; final_file_$i.txt
  done
  </code></pre>
  
  <p>("file_list" contains the files' names) But I keep receiving the error "sed: -e expression #3, char 58: unterminated `s' command".</p>
  
  <p>I've also tried this kind of script to do that:</p>
  
  <pre><code>for i in $(cat file_list.txt); do
      mapfile &lt; file_$i.txt   
      array[$i]=${MAPFILE[@]}
  done
  
  for i in $(cat file_list.txt); do
      sed -e "s/%label/${array[$i]}/g"&lt; template.txt &gt; final_file_$i.txt
  done
  </code></pre>
  
  <p>In this case I don't receive errors warnings and the substitution is made, but without line breaks.</p>
  
  <p>Is there a way of making the substitution keeping the line breaks?</p>
  
  <p>Thank you.</p>