title: Replace one capture group with another with GNU sed (macOS) 4.4
body: <p>I've looked around some other peripheral questions and haven't been able to find a solution to my problem, so I'm sorry if this is a duplicate to something I missed.</p>
  <p>Basically, I have the following GNU sed command:</p>
  <pre><code>sed -E -imr 's/^(\w)+/(\w)+$/g' file
  </code></pre>
  <p>which is supposed to replace the first word of a line with the last word of the line.</p>
  <p>The first regex <code>^(\w)+</code> works great and matches the first word of each line. The problem is that the command replaces that first word with the literal string <code>(w)+$</code></p>
  <p>I've tried to escape the backslash, the parentheses, the operators, but I've had no luck in making the regex work in the output part of the command.</p>
  <p>Can one use a regex capture group to replace a different regex capture group? What needs to be escaped, or what alternative syntax needs to be used?</p>
  <h1>NOTE</h1>
  <p>I'm using GNU sed on macOS from brew installed coreutils, so the answers to this question might not work across other versions of sed like native BSD on macOS.</p>