title: sed - replace pattern with content of file, while the name of the file is the pattern itself
body: <p>Starting from the <a href="https://stackoverflow.com/q/65637524/10988580">previous question</a>, I have another one. If I make this work, I can just delete several lines of script :D</p>
  <p>I want to  transform this line:</p>
  <pre><code>sed -i -r -e &quot;/$(basename &quot;$token_file&quot;)/{r $token_file&quot; -e &quot;d}&quot; &quot;$out_dir_rug&quot;/rug.frag
  </code></pre>
  <p>into  this line:</p>
  <pre><code>sed -i -r -e &quot;/(##_[_a-zA-Z0-9]+_##)/{r $out_dir_frags_rug/\1&quot; -e &quot;d}&quot; &quot;$out_dir_rug&quot;/rug.frag
  </code></pre>
  <p>The idea is the following. Originally (the first line), I searched for some patterns, and then replaced  those patterns with their  associated files. The names of the files are the patterns themselves.</p>
  <p>Eample:</p>
  <p>Pattern: <code>##_foo_##</code></p>
  <p>File name: <code>##_foo_##</code></p>
  <p>Content of file <code>##_foo_##</code>:</p>
  <pre><code>first line of foo text
  second line of foo text
  </code></pre>
  <p>so the text</p>
  <pre><code>bar
  ##_foo_##
  bar
  </code></pre>
  <p>would become</p>
  <pre><code>bar
  first line of foo text
  second line of foo text
  bar
  </code></pre>
  <hr />
  <p>In  my second  attempt, I used <code>sed</code> for both locating the patterns, and for the actual replacement.</p>
  <p>The result is that the patterns are found, but replaced with pretty much nothing.</p>
  <p><strong>Is <code>sed</code>  supposed to be able to do the replacement I want? If yes, how should I change my command?</strong></p>
  <hr />
  <p>Note: a file usually has several different patterns (I call them tokens), and the same pattern may appear more than one time.</p>
  <p>So an input file might look like:</p>
  <pre><code>bar
  bar
  ##_foo_##
  bar
  ##_haa_##
  bar
  ##_foo_##
  and so on
  </code></pre>
  <hr />
  <p>I already tried to replace the <code>/</code> in the  address with <code>,</code>, to no useful result. Escaping the <code>/</code> in the path to  <code>\/</code> also does not help.</p>
  <p>I verified that the path to the replacement files is good by adding the next line, just before the <code>sed</code>:</p>
  <pre><code>echo &quot;$out_dir_frags_rug&quot;
  </code></pre>