title: sed backreferences returning their numerical index rather than their value
body: <p>Weird problem here that I don't seem to see repeated anywhere else, so posting here.  Thanks in advance.</p>
  
  <p>I have the following multiline sed code that is printing further sed and copy commands into a script (yep, using a script to insert code into a script).  The code looks like this:</p>
  
  <pre><code>sed -i -r '/(rpub )([$][a-zA-Z0-9])/i\  
  sed -i '\''/#PBS -N/d'\'' \1\
  cp \1 '"$filevariable"'' $masterscript
  </code></pre>
  
  <p>which is supposed to do the following:</p>
  
  <p>1.) Open the master script<br>
  2.) Navigate to each instance of rpub $[a-zA-Z0-9] in the script<br>
  3.) Insert the second line (sed) and third line (cp) as lines before the rpub instance, using \1 as a backreference of the matched $[a-zA-Z0-9] from step 1.  </p>
  
  <p>This works great; all lines print well enough in relation to each other. However, all of my \1 references are appearing explicitly, minus their backslashes.  So all of my \1's are appearing as 1.</p>
  
  <p>I know my pattern match specifications are working correctly, as they nail all instances of rpub $[a-zA-Z0-9] well enough, but I guess I'm just not understanding the use of backreferences.  Anyone see what is going on here?</p>
  
  <p>Thanks.  </p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>Special thanks to Ed Morton below, implemented the following, which gets me 99% closer, but I still can't close the gap with unexpected behavior:</p>
  
  <pre><code>awk -v fv="$filevariable" '
  match($0, /rpub( [$][[:alnum:]])/, a)
  {
  print "sed -i '\''/#PBS -N/d'\''", a[1]
  }
  1' "$masterscript" &gt; tmpfile &amp;&amp; mv tmpfile "$masterscript"
  </code></pre>
  
  <p>Note: I removed one of the multiline print statements, as it isn't important here.  But, as I said, though this gets me much closer I am still having an issue where the printed lines appear between every line in the masterscript; it is as if the matching function is considering every line to be a match.  This is my fault, as I should probably have specified that I'd like the following to occur:</p>
  
  <pre><code>stuff here
  stuff here
  rpub $name
  stuff here
  rpub $othername
  stuff here
  </code></pre>
  
  <p>would become:</p>
  
  <pre><code>stuff here
  stuff here
  inserted line $name
  rpub $name
  stuff here
  insertedline $othername
  rpub $othername
  </code></pre>
  
  <p>Any help would be greatly appreciated. Thanks!</p>