title: using command substitution inside a sed script, with arguments
body: <p>I am trying to write a short script in which I use sed to search a stream, then perform a substitution on the stream based on the results of a shell function, which requires arguments from sed, e.g.</p>
  
  <pre><code>#!/bin/sh
  
  function test {
      echo "running test"
      echo $1
      }
  
  sed -n -e "s/.*\(00\).*/$(test)/p" &lt; testfile.txt
  </code></pre>
  
  <p>where testfile.txt contains: </p>
  
  <pre><code>1234
  2345
  3006
  4567
  </code></pre>
  
  <p>(with newlines between each; they are getting removed by your sites formatting). So ok that script works for me (output "running test"), but obviously has no arguments passed to test. I would like the sed line to be something like:</p>
  
  <pre><code>sed -n -e "s/.*\(00\).*/$(test \1)/p" &lt; testfile.txt
  </code></pre>
  
  <p>and output:</p>
  
  <pre><code>running test
  00
  </code></pre>
  
  <p>So that the pattern matched by sed is fed as an argument to test. I didn't really expect the above to work, but I have tried every combination of $() brackets, backticks, and escapes I could think of, and can find no mention of this situation anywhere. Help?</p>