February 12 2013

About a month ago, I wrote a shell script to do some automated file transactions. As part of the scripting process, the last step required me to have certain files ready in a specific folder on a server to be consumed by other downstream systems. These downstream systems needed some of the data in these files to be modified for them, to be able to consume them successfully. The number of files created and the amount of data in these files would vary per execution of the shell script, but the data that needed to be changed was known to me.

So basically, I needed a way to do a Find and Replace on the file(s) before other systems could consume them. There are a lot of tools and scripts out there that can do a Find and Replace for me, but I chose ANT as the solution for a couple of reasons:

a) To be automated, so it can be made part of existing automated process
b) Not be environment and operating system specific, so I can use it in the future and not be tied to only using it within Unix
c) The Find-Replace pairs to be dynamic

What I mean by dynamic find-replace pairs is that, for every execution, any combination of these three could change:

a) The Find variable
b) The Replace variable
c) Number of Find-Replace pairs

Below are the steps to setup automated Find-Replace using ANT scripts:

1) Install ANT-Contrib

ANT alone is not enough; we need ANT-Contrib. The ANT-Contrib project is a collection of tasks for Apache ANT. More information and how to install it can be found at the following link: http://ant-contrib.sourceforge.net/#intro

2) Create a key-value pair property file

In scenarios like the above where you have static or dynamic key-value pairs, it is always advisable to create a separate property file that holds those pairs. We are going to setup our property file (myprop.properties) with what we want to Find and what we want to Replace it with, using key(Find)-value(Replace) pairs. The below is a snippet of the key-value pairs property file, where samplepairs is our Find helper that does not change. oldvalue is our Find variable and newvalue is our Replace variable, and these two might change per execution.

3) Include property file

Include Property file in build.xml where the ANT tasks (Step 4) will reside, so it can refer to the property file (Step 2).

4) Create the ANT tasks

The ANT task, myreplace is created within build.xml. This task gets all the keys and iterates through each key in a foreach loop.
The ANT-Contrib task that is used to get the keys is propertyselector. More information on this task can be found at this link: http://ant-contrib.sourceforge.net/tasks/tasks/propertyselector.html
These are the propertyselector attributes used in the code below:
property: The name of the property you wish to set.
match: The regular expression which is used to select property names for inclusion in the list. This follows the standard regular expression syntax accepted by ANT's regular expression tasks.
select: A pattern which indicates what selection pattern you want in the returned list. This used the substitution pattern syntax to indicate where to insert groupings created as a result of the regular expression match.
casesensitive: Should the match be case sensitive.
delimiter: The delimiter used to separate entries in the resulting property.

The ANT task, myreplaceeach is created within build.xml as well. This task gets the value corresponding to the key provided and then iterates through each file in the specified folder (yourSeachFolder) and replaces the key with the value.
The ANT-Contrib task that is used is propertycopy. More information on this task can be found at this link: http://ant-contrib.sourceforge.net/tasks/tasks/propertycopy.html
These are the propertycopy attributes used in the code below:
property: The name of the property you wish to set.
from: The name of the property you wish to copy the value from.

5) Confirm key-value pair

Before the execution of the above ANT task, the only thing you will need to confirm is your myprop.properties file is set with the correct key-value pairs. 
Note: If you have key-value pairs where the key is not found in the search folder during the execution, it will not stop or break the ANT execution.

6) Execute the ANT task

Execute the ANT task, myreplace to do the automated Find-Replace. The code below executes the ANT task myreplace that writes the output in verbose mode (-v) to the log file specified.

ant -v myrepace > ../logs/mylog.out

Once the ANT task execution finishes, it should have first iterated through each key in the myprop.properties file and then it should have iterated through each file in the folder specified above (yourSeachFolder) and replaced the key (Find) with the corresponding value (Replace). If you have any questions or comments, please feel free to drop me a line below.

Join the Conversation

October 16, 2013

Finaly found this article, really really needed it. Awesome stuff! Keep up the good work!

December 21, 2013

Thanks Rajan.

August 22, 2014

Nice Article !!!!

April 7, 2015

I cannot execute this code, I get an error that sp.key is not defined. any suggestions?

August 26, 2015

My question is can ant dynamically pick the property file?

I have two property files and my shell script exports an env variable which can be used to determine which property file need to be used.

Does ant support below syntax ( I am not good at ant so below is pesudo code):

if ${env.A} = 'Y'

   <property file="propertyfile1"/>

else

   <property file="propertyfile2"/>

Thank you very much for your help!

Yi

November 19, 2015

Kudos!!! Awesome Job.

December 1, 2015

does it support UTF-8 format string replace?

gta money
August 5, 2017

It looks like author is very creative. these are very classy blogs. Nice and very informative posts to explore gta online generator. Keep sharing more to spread good ideas among people.

Enter your first name. It will only be used to display with your comment.
Enter your email. This will be used to validate you as a real user but will NOT be displayed with the comment.
By submitting this form, you accept the Mollom privacy policy.