title: Could this complex find-and-replace be done as a one-liner?
body: <p>Suppose I have the following text:   (note that the examples have been simplified since they were originally written)</p>
  
  <pre><code>\begin{align} E_{i,j} \left( \Phi_{i-1,j-1} + \Phi_{i-1,j} + \Phi_{i-1,j+1} + \Phi_{i,j-1} + \Phi_{i,j} + \Phi_{i,j+1} + \Phi_{i+1,j-1} + \Phi_{i+1,j} + \Phi_{i+1,j+1} \right) \end{align}
  </code></pre>
  
  <p>I have two tasks: </p>
  
  <ul>
  <li>Replace each <code>i</code> in the brackets with a given value (e.g. <code>2</code>). </li>
  <li>Replace each <code>j</code> in the brackets with a given value (e.g. <code>6</code>). </li>
  </ul>
  
  <p>Constraints:</p>
  
  <ul>
  <li>The script should not naively replace <em>any</em> <code>i</code> or <em>any</em> <code>j</code>, but only the ones within the <code>_{*,*}</code> pattern. </li>
  <li>There are other constructs between braces (e.g. <code>\begin{align}</code>) that should not be affected. </li>
  <li>Wherever there is a mathematical operator (e.g. <code>i+1</code>, <code>j-2</code>, etc.), the replacement should be the numeric result of the operation. (The math would only ever be addition or subtraction.) </li>
  <li>All instances within a line should be replaced -- not just the first instance on a line. </li>
  <li>I could also replace <code>i</code> or <code>j</code> with a alphanumeric pattern (e.g. <code>i</code> --> <code>N_r</code>. In this case, the substitution is simple and straightforward. </li>
  <li>I want to be able to replace either <code>i</code>, or <code>j</code>, <em>or both</em> with values. </li>
  <li>I need to do this on multiple lines. (I use a text file.) </li>
  </ul>
  
  <h3>Example outputs</h3>
  
  <p>The example text (above), with <code>i</code> --> <code>3</code>, would become:</p>
  
  <pre><code>\begin{align} E_{3,j} \left( \Phi_{2,j-1} + \Phi_{2,j} + \Phi_{2,j+1} + \Phi_{3,j-1} + \Phi_{3,j} + \Phi_{3,j+1} + \Phi_{4,j-1} + \Phi_{4,j} + \Phi_{4,j+1} \right) \end{align}
  </code></pre>
  
  <p>The example text (above), with <code>j</code> --> <code>5</code>, would become:</p>
  
  <pre><code>\begin{align} E_{i,5} \left( \Phi_{i-1,4} + \Phi_{i-1,5} + \Phi_{i-1,6} + \Phi_{i,4} + \Phi_{i,5} + \Phi_{i,6} + \Phi_{i+1,4} + \Phi_{i+1,5} + \Phi_{i+1,6} \right) \end{align}
  </code></pre>
  
  <p>The example text (above), with <code>i</code> --> <code>3</code> AND <code>j</code> --> <code>5</code>, would become:</p>
  
  <pre><code>\begin{align} E_{3,5} \left( \Phi_{2,4} + \Phi_{2,5} + \Phi_{2,6} + \Phi_{3,4} + \Phi_{3,5} + \Phi_{3,6} + \Phi_{4,4} + \Phi_{4,5} + \Phi_{4,6} \right) \end{align}
  </code></pre>
  
  <p>The example text (above), with <code>i</code> --> <code>N_r</code>, would become:</p>
  
  <pre><code>\begin{align} E_{N_r,j} \left( \Phi_{N_r-1,j-1} + \Phi_{N_r-1,j} + \Phi_{N_r-1,j+1} + \Phi_{N_r,j-1} + \Phi_{N_r,j} + \Phi_{N_r,j+1} + \Phi_{N_r+1,j-1} + \Phi_{N_r+1,j} + \Phi_{N_r+1,j+1} \right) \end{align} 
  </code></pre>
  
  <h3>My question: Can this be done as a one-liner (perhaps using <code>awk</code>), and if so, how?</h3>
  
  <p>If not, how few lines would it take in either <code>awk</code>, <code>sed</code>, or <code>python</code> (or similar)? </p>
  
  <p>I know python handles command-line parameters well and could easily evaluate the math, but the tokenization seems hard. </p>
  
  <p>I think my difficulty is (1) how to get <code>sed</code> or <code>awk</code> to take a variable number of command-line parameters (the substitutions; either one or both), and (2) how to get the behavior to change between a substitution being numeric (and having to do math) and symbolic (and having the substitution be text-only). </p>
  
  <p>Tie-breaker goes to the version that can go backwards for symbolic characters. For example, if the input text were the following:</p>
  
  <pre><code>\begin{align} E_{N_r,j} \left( \Phi_{N_r-1,j-1} + \Phi_{N_r-1,j} + \Phi_{N_r-1,j+1} + \Phi_{N_r,j-1} + \Phi_{N_r,j} + \Phi_{N_r,j+1} + \Phi_{N_r+1,j-1} + \Phi_{N_r+1,j} + \Phi_{N_r+1,j+1} \right) \end{align}
  </code></pre>
  
  <p>and I wanted to replace <code>N_r</code> with <code>i</code>, then the result would be the following:</p>
  
  <pre><code>\begin{align} E_{N_r,j} \left( \Phi_{N_r-1,j-1} + \Phi_{N_r-1,j} + \Phi_{N_r-1,j+1} + \Phi_{N_r,j-1} + \Phi_{N_r,j} + \Phi_{N_r,j+1} + \Phi_{N_r+1,j-1} + \Phi_{N_r+1,j} + \Phi_{N_r+1,j+1} \right) \end{align}
  </code></pre>
  
  <p>Bonus points for being able to replace symbols with other symbols that may include math, and doing the math right. For example, it should replace <code>i</code> with <code>i-1</code>, such that if it finds <code>_{i-1,j}</code>, it converts it into ``_{i-2,j}`. </p>