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.