extracting value from a file using ant

11,954

Solution 1

I think this is what you are interested in. On an input file like below

Number of current user build lists: 23
'RevisionBuild' Run time (in minutes) = 8.40
[Build] RC = 0

when I execute below target

<project name="BuildModule" basedir="." default="extract.nums">
    <taskdef resource="net/sf/antcontrib/antlib.xml" />
    <property environment="env" />
    <loadfile property="file" srcfile="${basedir}/inputLog.log"/>
    <target name="extract.nums">
        <for param="line" delimiter="${line.separator}" list="${file}">
            <sequential>
                <propertyregex property="noOfBuildlists" input="@{line}" regexp="Number of current user build lists:\s*([0-9]+)$" select="\1" />
                <propertyregex property="revisionBuild" input="@{line}" regexp="'RevisionBuild' Run time\s*\(in minutes\)\s*=\s*([0-9\.]+)$" select="\1" />
                <propertyregex property="rcBuild" input="@{line}" regexp="\[Build\] RC\s*\=\s*([0-9]+)$" select="\1" />
            </sequential>
        </for>
        <echo message="Current user build : ${noOfBuildlists}" />
        <echo message="Revision Build : ${revisionBuild}" />
        <echo message="RC Build : ${rcBuild}" />
    </target>
</project>

I get below output.

[echo] Current user build : 23
[echo] Revision Build : 8.40
[echo] RC Build : 0

Solution 2

When I use propertyregex, it prompt an error if doesn't support the nested "condition" element. There are many nested "condition" element in my {android.sdk.air}/tools/ant/build.xml. So I tried to find a solution that standard Ant can present. I made it.

Try This:

<loadfile
    encoding="UTF-8"
    property="property.for.your.string"
    srcFile="your/file/path" >
        <filterchain>
            <tokenfilter>
                <containsregex
                    pattern="Number of current user build lists:\s*([0-9]+)$"
                    replace="\1" />
            </tokenfilter>
        </filterchain>
</loadfile>

<echo message="DEBUG LOG: property.for.your.string = ${property.for.your.string}" />

It makes me wonder why ant name the attribute as "replace", which leads to my misunderstanding.

Share:
11,954
user1384205
Author by

user1384205

Updated on June 12, 2022

Comments

  • user1384205
    user1384205 almost 2 years

    The following lines are present in the file. I would like to extract multiple data from this file.

    Number of current user build lists: 23

    • 'RevisionBuild' Run time (in minutes) = 8.40

    [Build] RC = 0

    I used the following regex to retrieve the value 23 from this file

    <ac:for param="line" list="${logFileContent}" delimiter="${line.separator}">
      <sequential>                          
    <propertyregex property="noOfBuildlists"
             input="@{line}"
         regexp="(.*)Number of current user build lists: (.*)$"
         select="\2"/>  
      </sequential>
    </ac:for>
    

    But the same regex does not fetch any value when i try to fetch the other lines such as regexp="(.)[revBuild] RC = (.)$" or regexp="(.)'RevisionBuild' Run time (in minutes) = (.)$" where the extracted values should be 0 and 8.40 respectively.

    Can someone please help? Thanks, Aarthi