title: Using placeholders/variables in a sed command
body: <p>I want to store a specific part of a matched result as a variable to be used for replacement later. I would like to keep this in a one liner instead of finding the variable I need before hand.</p>
  
  <p>when configuring apache, and use mod_rewrite, you can specificy specific parts of patterns to be used as variables,like this:</p>
  
  <pre><code>RewriteRule ^www.example.com/page/(.*)$ http://www.example.com/page.php?page=$1 [R=301,L]
  </code></pre>
  
  <p>the part of the pattern match that's contained inside the parenthesis is stored as $1 for use later. So if the url was <code>www.example.com/page/home</code>, it would be replaced with <code>www.example.com/page.php?page=home</code>. So the "home" part of the match was saved in $1 because it was the part of the pattern inside the parenthesis.</p>
  
  <p>I want something like this functionality with a <code>sed</code> command, I need to automatically replace many strings in a SQL dump file, to add drop table if exist commands before each create table, but I need to know the table name to do this, so if the dump file contains something like:</p>
  
  <pre><code>...
  CREATE TABLE `orders`
  ...
  </code></pre>
  
  <p>I need to run something like:</p>
  
  <pre><code>cat dump.sql | sed "s/CREATE TABLE `(.*)`/DROP TABLE IF EXISTS $1\N CREATE TABLE `$1`/g"
  </code></pre>
  
  <p>to get the result of:</p>
  
  <pre><code>...
  DROP TABLE IF EXISTS `orders`
  CREATE TABLE `orders`
  ...
  </code></pre>
  
  <p>I'm using the mod_rewrite syntax in the <code>sed</code> command as a logical example of what I'm trying to do.</p>
  
  <p>Any suggestions?</p>