title: Replace matching pairs of characters with parenthesis
body: <p>It is better to describe an example. I have a latex source file (this is an ordinary text file) that has a lot of charactes <code>$</code> enclosing inline equations, something like this:</p>
  
  <pre><code>bla bla bla $E = mc^2$ bla blah
  </code></pre>
  
  <p>I would like to replace each ocurrence of a <em>matching pair</em> of <code>$</code> characters in the file by <code>\( ... \)</code>, like this:</p>
  
  <pre><code>bla bla bla \(E = mc^2\) bla blah
  </code></pre>
  
  <p>Any idea of to do this, as simple as possible? I am not sure grep is able to handle this.</p>
  
  <p>Assume that the file has an <em>even</em> number of occurrences of <code>$</code>. In that case, all we have to do is replace the <code>$</code> at odd positions by <code>\(</code>, and the <code>$</code> at even positions by <code>\)</code>.</p>