title: Sed and grep regex syntaxes differ
body: <p>This is a part of my shell script, which I use to perform a recursive find and replace in the working directory. Backup and other utilities are in other functions, which are irrelevant to my problem.</p>
  
  <pre><code>#!/bin/bash
  
  # backup function goes here
  
  # @param $1 The find pattern.
  # @param $2 The replace pattern. 
  function findAndReplace {
      bufferFile=/tmp/tmp.$$
      filesToReplace=`find . -type f | grep -vi cvs | grep -v '#'`
      sedPattern="s/$1/$2/g"
      echo "Using pattern $sedPattern"
      for f in $filesToReplace; do
          echo "sedding file $f"
          sed "$sedPattern" "$f" &gt; "$bufferFile"
          exitCode=$?
          if [ $exitCode -ne 0 ] ; then
              echo "sed $sedPattern exited with $exitCode"
              exit 1
          fi
          chown --reference=$f $bufferFile
          mv $bufferFile $f
      done
  }
  
  backup
  findAndReplace "$1" "$2"
  </code></pre>
  
  <p>Here's a sample usage: <code>recursive-replace.sh "function _report" "function report"</code>.</p>
  
  <p>It works, but there is one problem. It uses <em>sed</em> on ALL files in the working directory. I would like to <em>sed</em> only those files, which contain the find pattern.</p>
  
  <p>Then, I modified the line:</p>
  
  <pre><code>filesToReplace=`find . -type f | grep -vi cvs | grep -v '#'`
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>filesToReplace=`grep -rl "$1" . | grep -vi cvs | grep -v '#'`
  </code></pre>
  
  <p>And it works too, but not for all find patterns. E.g. for pattern <code>\$this-&gt;report\((.*)\)</code> I recieve error: <code>grep: Unmatched ( or \(</code>. This pattern is correct for <em>sed</em>, but not for <em>grep</em>.
  Regex syntaxes for <em>grep</em> and <em>sed</em> differ. What can I do?</p>