phing

find replace text in file with Phing


Does anyone know how to find and replace text inside a file with Phing?


Solution

  • The simplest way to achieve this using 'traditional' tools would be sed:

    sed -i 's/old/new/g'  myfile.txt
    

    And if it is ant-based then this should help: http://ant.apache.org/manual/Tasks/replace.html

    The simplest form would be <replace file="myfile.html" token="OLD" value="NEW"/>.

    And if you really need it, you could run external tools with ant as documented at http://ant.apache.org/manual/Tasks/exec.html, which means that among other things you could, for example, call sed from ant with something like:

     <exec executable="sed">
       <arg value="s/old/new/g" />
       <arg value="$MY_FILE" />
     </exec>