find replace text in file with Phing
Asked Answered
C

6

16

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

Colchicine answered 5/2, 2011 at 14:34 Comment(0)
C
1

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>
Clausius answered 5/2, 2011 at 15:56 Comment(2)
PHing Is a PHP project build system or build tool based on Apache Ant. You can do anything with it that you could do with a traditional build system like GNU make, and its use of simple XML build files and extensible PHP "task" classes make it an easy-to-use and highly flexible build framework.Colchicine
I tried the replace tag but getting Could not create task/type: 'replace'. Make sure that this class has been declared using taskdef / typedef.Berkelium
L
30

If you don't want to copy files and just replace a string in the current folder where your files reside, do a reflexive task:

<reflexive>
    <fileset dir=".">
        <include pattern="*.js" />
    </fileset>
    <filterchain>
        <replaceregexp>
            <regexp pattern="SEARCH" replace="REPLACEMENT"/>
        </replaceregexp>
    </filterchain>
</reflexive>
Lyndsaylyndsey answered 8/11, 2011 at 9:20 Comment(2)
Thanks! This answer is really the best option though other have more votes at this point. This how the task should be completed without unnecessary copying and and using exec task when native phing task is available.Pterous
Agree with the previous comment. This is the answer that should be voted at most as it actually answers the question, all others are workarounds.Zane
A
25

You can replace text inside files using filters. Filters are used inside other file operation tasks such as copy.

I believe the main idea behind filters is that you can have template files with tokens instead of real values and you then substitute the tokens as a part of the copy process.

Quick example: have a database configuration template file stored in a template directory. Then you copy it to the target configuration file using:

<copy file="templates/database.config.php.tpl" tofile="config/database.config.php" overwrite="true">
                <filterchain>
                    <replacetokens begintoken="%%" endtoken="%%">
                        <!-- MySQL TOKENS -->
                        <token key="dbname" value="${db.mysql.dbname}" />
                        <token key="dbhost" value="${db.mysql.host}" />
                        <token key="dbport" value="${db.mysql.port}" />
                        <token key="dbuser" value="${db.mysql.username}" />
                        <token key="dbpassword" value="${db.mysql.password}" />
                    </replacetokens>
                </filterchain>
            </copy>

There are plenty of other filters (e.g. regex search and replace) available. See more about filters in the documentation: http://phing.info/docs/guide/stable/chapters/appendixes/AppendixD2-CoreFilters.html

Almucantar answered 6/2, 2011 at 15:36 Comment(0)
P
8

I was looking for the same thing, and I found out that exists a filter named ExpandProperties which allows to replace properties in the copied file. For example I used it in a apache virtual host template:

<target name="apache-config" description="Generates apache configuration">
    <!-- Default value for Debian/Ubuntu -->
    <property name="apache.vhost.dir" value="/etc/apache2/sites-available" override="false"/>
    <copy file="${application.startdir}/docs/vhost.conf.tpl" todir="${apache.vhost.dir}" overwrite="true">
        <filterchain>
            <expandproperties/>
        </filterchain>
    </copy>
    <echo message="Apache virtual host configuration copied, reload apache to activate it"/>
</target>

And in the template file

<VirtualHost *:80>
   DocumentRoot "${application.startdir}/public"
   ServerName ${apache.default.host}

   <Directory "${application.startdir}/public">
       Options Indexes MultiViews FollowSymLinks
       AllowOverride All
       Order allow,deny
       Allow from all
   </Directory>

</VirtualHost>

In this way you don't need to explicitly list all the tokens you want replaced, pretty useful...

Pigmy answered 19/7, 2011 at 11:34 Comment(1)
I used your filter chain, but with the above person's reflexive tasks.Amado
W
2

I use this on my phing build.xml file

<exec command="find ./ -type f -name '*.php' | xargs sed -i 's|x--Jversion--x|${jversion}|g'" dir="${targetdir}/_package/${extname}.${package.version}" /> 
Warrick answered 29/3, 2012 at 9:25 Comment(1)
x--Jversion--x is the Placeholder ${jversion} is the new text.Warrick
C
1

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>
Clausius answered 5/2, 2011 at 15:56 Comment(2)
PHing Is a PHP project build system or build tool based on Apache Ant. You can do anything with it that you could do with a traditional build system like GNU make, and its use of simple XML build files and extensible PHP "task" classes make it an easy-to-use and highly flexible build framework.Colchicine
I tried the replace tag but getting Could not create task/type: 'replace'. Make sure that this class has been declared using taskdef / typedef.Berkelium
V
-1

The answer given by Acme, is the right one. If you try to copy a file to itself in order to modify it, yells telling that yo cannot self-copy.

<reflexive file="./app/config/config.yml" tofile="./app/config/config.yml">
    <filterchain>
    <replacetokens begintoken="__" endtoken="__">
        <token key="BUILD_VERSION" value="Replace Value" />
    </replacetokens>
    </filterchain>
</reflexive>

This works well for me.

Volute answered 19/1, 2017 at 13:45 Comment(1)
If your example you would only need: <reflexive file="./app/config/config.yml" >Sightseeing

© 2022 - 2024 — McMap. All rights reserved.