Paul Taylor
Paul Taylor

Reputation: 13110

Using ant to modify a file containing values in another file

I'm currently using ant to remove lines from a file if the line matches any of a list of email addresses, and output a new file without these email addresses as follows:

<copy file="src/emaillist.tmp2" tofile="src/emaillist.txt">
   <filterchain>
       <linecontains negate="true"><contains value="[email protected]"/>     
       </linecontains>
       <linecontains negate="true"><contains value="[email protected]"/>           
       </linecontains>
            ........

            ........
   </filterchain>
</copy>

But I already have a file containing a list of the invalid email addresses (invalidemail.txt) I want to remove, so I want my ant file to read the list of invalid email addresses from this file rather than having to add a element for each email I don't want. Cannot work out how to do this.

Upvotes: 2

Views: 158

Answers (1)

martin clayton
martin clayton

Reputation: 78115

Ant has some useful resource list processing tasks. Here's a 'prototype' solution.

Load the input and exclusion lists to two resource collections, tokenized by default on line breaks.

<tokens id="input.list">
    <file file="src/emaillist.tmp2"/>
</tokens>
<tokens id="invalid.list">
    <file file="invalidemail.txt"/>
</tokens>

Do some set arithmetic to produce a resource list of clean emails:

<intersect id="to_be_removed.list">
    <resources refid="input.list"/>
    <resources refid="invalid.list"/>
</intersect>

<difference id="clean.list">
    <resources refid="input.list"/>
    <resources refid="to_be_removed.list"/>
</difference>

Here's some diagnostics that may be useful:

<echo message="The input list is: ${ant.refid:input.list}" />
<echo message="Emails to be removed: ${ant.refid:to_be_removed.list}" />
<echo message="The clean list is: ${ant.refid:clean.list}" />

Use the pathconvert task to reformat the clean list into one entry per line:

<pathconvert property="clean.prop" refid="clean.list"
             pathsep="${line.separator}" />

Finally, output to a file:

<echo file="src/emaillist.txt">${clean.prop}
</echo>

Upvotes: 1

Related Questions