Quantcast

Using ant to parse text file

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Using ant to parse text file

DHARNA, AJAY [AG/1000]
Hi all

I am using Ant 1.6.5 - I would like to parse a text file that would contain
the following information:

[Sample.txt]
Test1
Group=red

Test2
Group=blue

Test3
Group=green

Example:- If I was to provide the value "Test2", I would like the Ant to
search for "Test2" in the file, and return the value "blue". If I provide
the value - "Test3", it should return the value "green"

Please let me know if there is any simple way of doing this using Ant.

Thank you

Ajay Dharna


---------------------------------------------------------------------------------------------------------
This e-mail message may contain privileged and/or confidential information, and is intended to be received only by persons entitled to receive such information. If you have received this e-mail in error, please notify the sender immediately. Please delete it and all attachments from any servers, hard drives or any other media. Other use of this e-mail by you is strictly prohibited.


All e-mails and attachments sent and received are subject to monitoring, reading and archival by Monsanto. The recipient of this e-mail is solely responsible for checking for the presence of "Viruses" or other "Malware". Monsanto accepts no liability for any damage caused by any such code transmitted by or accompanying this e-mail or any attachment.
---------------------------------------------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: Using ant to parse text file

Jan.Materne
I dont know any way (without writing a custom task).

Jan

>-----Urspr√ľngliche Nachricht-----
>Von: DHARNA, AJAY [AG/1000] [mailto:[hidden email]]
>Gesendet: Donnerstag, 22. Februar 2007 07:04
>An: Ant Users List
>Betreff: Using ant to parse text file
>
>Hi all
>
>I am using Ant 1.6.5 - I would like to parse a text file that
>would contain
>the following information:
>
>[Sample.txt]
>Test1
>Group=red
>
>Test2
>Group=blue
>
>Test3
>Group=green
>
>Example:- If I was to provide the value "Test2", I would like
>the Ant to
>search for "Test2" in the file, and return the value "blue".
>If I provide
>the value - "Test3", it should return the value "green"
>
>Please let me know if there is any simple way of doing this using Ant.
>
>Thank you
>
>Ajay Dharna
>
>
>---------------------------------------------------------------
>------------------------------------------
>This e-mail message may contain privileged and/or confidential
>information, and is intended to be received only by persons
>entitled to receive such information. If you have received
>this e-mail in error, please notify the sender immediately.
>Please delete it and all attachments from any servers, hard
>drives or any other media. Other use of this e-mail by you is
>strictly prohibited.
>
>
>All e-mails and attachments sent and received are subject to
>monitoring, reading and archival by Monsanto. The recipient of
>this e-mail is solely responsible for checking for the
>presence of "Viruses" or other "Malware". Monsanto accepts no
>liability for any damage caused by any such code transmitted
>by or accompanying this e-mail or any attachment.
>---------------------------------------------------------------
>------------------------------------------
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using ant to parse text file

Kevin Jackson-3
In reply to this post by DHARNA, AJAY [AG/1000]
> [Sample.txt]
> Test1
> Group=red
>
> Test2
> Group=blue
>
> Test3
> Group=green
>
> Example:- If I was to provide the value "Test2", I would like the Ant to
> search for "Test2" in the file, and return the value "blue". If I provide
> the value - "Test3", it should return the value "green"
>
> Please let me know if there is any simple way of doing this using Ant.

Given that you don't seem to have any more data for each of the Tests
- why don't you just do

Test1=red

then you can use Ant's property task

Even if you are going to group things together you can still use
properties just separate the properties into separate files and then
when you provide Test2, Ant can then lookup the appropraitely names
Test2.properties and then load the values

Unless you want to do something else, I think you want the properties task

Kev

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Using ant to parse text file

DHARNA, AJAY [AG/1000]
In reply to this post by DHARNA, AJAY [AG/1000]
I have looked at the property task as well - but I will not be able to
change the file format.

I used the following target to get "Test2"

<target name="getGroupValue">
    <loadfile srcfile="${src.file}" property="text">
      <filterchain>
        <linecontains>
          <contains value="Test2"/>
        </linecontains>
      </filterchain>
    </loadfile>
    <echo>${text}</echo>
  </target>

Is there anyway to get back the line number where this is found - If I
can get back the line number - I could use the headfilter to get this
next line.

Thank you

Ajay Dharna


-----Original Message-----
From: Kevin Jackson [mailto:[hidden email]]
Sent: Thursday, February 22, 2007 1:59 AM
To: Ant Users List
Subject: Re: Using ant to parse text file


> [Sample.txt]
> Test1
> Group=red
>
> Test2
> Group=blue
>
> Test3
> Group=green
>
> Example:- If I was to provide the value "Test2", I would like the Ant
> to search for "Test2" in the file, and return the value "blue". If I
> provide the value - "Test3", it should return the value "green"
>
> Please let me know if there is any simple way of doing this using Ant.

Given that you don't seem to have any more data for each of the Tests
- why don't you just do

Test1=red

then you can use Ant's property task

Even if you are going to group things together you can still use
properties just separate the properties into separate files and then
when you provide Test2, Ant can then lookup the appropraitely names
Test2.properties and then load the values

Unless you want to do something else, I think you want the properties
task

Kev

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------------------------------------------
This e-mail message may contain privileged and/or confidential information, and is intended to be received only by persons entitled to receive such information. If you have received this e-mail in error, please notify the sender immediately. Please delete it and all attachments from any servers, hard drives or any other media. Other use of this e-mail by you is strictly prohibited.


All e-mails and attachments sent and received are subject to monitoring, reading and archival by Monsanto. The recipient of this e-mail is solely responsible for checking for the presence of "Viruses" or other "Malware". Monsanto accepts no liability for any damage caused by any such code transmitted by or accompanying this e-mail or any attachment.
---------------------------------------------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using ant to parse text file

Ninju Bohra
In reply to this post by DHARNA, AJAY [AG/1000]
Then I would look at writing a quick custom task  (they really aren't that hard) so that you can eliminate all the details of the file structure to within the task.

I would see something like:

<getGroupValue filename="..." lookfor="...." setproperty="result"/>

You would pass in (as attributes) the filename, the lookfor value (i.e. 'Test2'), and the name of the property that would be defined whose value will the group that was found.

This is a lot cleaner than exposing/manipulating the file (and it rigid structure) through a series of generic ANT tasks.

Just my $.02,

Ninju


----- Original Message ----
From: "DHARNA, AJAY [AG/1000]" <[hidden email]>
To: Ant Users List <[hidden email]>
Sent: Thursday, February 22, 2007 9:58:05 AM
Subject: RE: Using ant to parse text file


I have looked at the property task as well - but I will not be able to
change the file format.

I used the following target to get "Test2"

<target name="getGroupValue">
    <loadfile srcfile="${src.file}" property="text">
      <filterchain>
        <linecontains>
          <contains value="Test2"/>
        </linecontains>
      </filterchain>
    </loadfile>
    <echo>${text}</echo>
  </target>

Is there anyway to get back the line number where this is found - If I
can get back the line number - I could use the headfilter to get this
next line.

Thank you

Ajay Dharna


-----Original Message-----
From: Kevin Jackson [mailto:[hidden email]]
Sent: Thursday, February 22, 2007 1:59 AM
To: Ant Users List
Subject: Re: Using ant to parse text file


> [Sample.txt]
> Test1
> Group=red
>
> Test2
> Group=blue
>
> Test3
> Group=green
>
> Example:- If I was to provide the value "Test2", I would like the Ant
> to search for "Test2" in the file, and return the value "blue". If I
> provide the value - "Test3", it should return the value "green"
>
> Please let me know if there is any simple way of doing this using Ant.

Given that you don't seem to have any more data for each of the Tests
- why don't you just do

Test1=red

then you can use Ant's property task

Even if you are going to group things together you can still use
properties just separate the properties into separate files and then
when you provide Test2, Ant can then lookup the appropraitely names
Test2.properties and then load the values

Unless you want to do something else, I think you want the properties
task

Kev

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------------------------------------------
This e-mail message may contain privileged and/or confidential information, and is intended to be received only by persons entitled to receive such information. If you have received this e-mail in error, please notify the sender immediately. Please delete it and all attachments from any servers, hard drives or any other media. Other use of this e-mail by you is strictly prohibited.


All e-mails and attachments sent and received are subject to monitoring, reading and archival by Monsanto. The recipient of this e-mail is solely responsible for checking for the presence of "Viruses" or other "Malware". Monsanto accepts no liability for any damage caused by any such code transmitted by or accompanying this e-mail or any attachment.
---------------------------------------------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


 
____________________________________________________________________________________
It's here! Your new message!  
Get new email alerts with the free Yahoo! Toolbar.
http://tools.search.yahoo.com/toolbar/features/mail/

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

SQL file in Ant 1.7

Chun Ji
In reply to this post by DHARNA, AJAY [AG/1000]

Here is a target I created to execute some sql files in Ant 1.7
" <target ...>
      <sql  driver="${oracle.driver}"
              url="${oracle.url}"
              userid="${oracle.user}"
              password="${oracle.pwd}"
            keepformat="true">
         <path>
            <fileset dir=".">
               <include name="*.sql"/>
            </fileset>
         </path>
   </sql>
  </target>
".

As far as I can see, when this target is being executed, it gives me messages such as:
"....
100 SQL statements executed successfully
...
",
But does it mean all the SQLs also have been compiled successfully ?


Chun



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: SQL file in Ant 1.7

Karl Heinz Marbaise-3
Hello,


> As far as I can see, when this target is being executed, it gives me messages such as:
> "....
> 100 SQL statements executed successfully
> But does it mean all the SQLs also have been compiled successfully ?
First the SQL file will not be compiled.
Second this means the all statements within all SQL files you've
selected there have 100 SQL statements. So alle these statements have
been exectued successfully on the database.

Kind regards
Karl Heinz Marbaise
--
SoftwareEntwicklung Beratung Schulung        http://www.soebes.de
Dipl.Ing.(FH) Karl Heinz Marbaise            email: [hidden email]
Tel.: +49 (0) 2405 / 415 893                  ICQ#: 135949029

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: SQL file in Ant 1.7

Chun Ji
In reply to this post by Chun Ji


-----Original Message-----
From: Karl Heinz Marbaise [mailto:[hidden email]]
Sent: Thursday, February 22, 2007 12:02 PM
To: Ant Users List
Subject: Re: SQL file in Ant 1.7
Importance: Low


Hello,


> As far as I can see, when this target is being executed, it gives me messages such as:
> "....
> 100 SQL statements executed successfully
> But does it mean all the SQLs also have been compiled successfully ?
First the SQL file will not be compiled.
Second this means the all statements within all SQL files you've
selected there have 100 SQL statements. So alle these statements have
been exectued successfully on the database.




Sorry I did not make it clear.
We are trying to upload some packages, stored procedures on a new database. And these head, body files are saved in these sql file and those do need to be compiled after uploaded. Basically I try to find some compiling information when the above target is executed.


Chun




 

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using ant to parse text file

Kevin Jackson-3
In reply to this post by Ninju Bohra
Hi,

If you don't want to write a custom task in java, you could also try
<scriptdef> and use a scripting language of your choice

http://ant.apache.org/manual/OptionalTasks/scriptdef.html

Kev

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: Using ant to parse text file

Jan.Materne
In reply to this post by DHARNA, AJAY [AG/1000]
I played a little bit ...

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;


public class LoadGroupTask extends Task {

        String prefix;
        File   file;
        String section;
       
        @Override
        public void execute() throws BuildException {
                validate();
                BufferedReader rdr;
                try {
                        rdr = new BufferedReader(new FileReader(file));
                        boolean needThisSection = false;
                        String line = rdr.readLine();
                        while (line!=null) {
                                line = line.trim();
                                if (line.length()>0) {
                                        if (line.indexOf("=") > 0) {
                                                if (needThisSection) {
                                                        String[] parts = line.split("=");
                                                        String propName  = (prefix==null)   ? parts[0] : prefix + parts[0];
                                                        String propValue = (parts[1]!=null) ? parts[1] : "";
                                                        log("Setting property " + propName + " -> " + propValue);
                                                        getProject().setNewProperty(propName, propValue);
                                                }
                                        } else {
                                                needThisSection = section.equals(line);
                                        }
                                }
                                line = rdr.readLine();
                        }
                        rdr.close();
                } catch (Exception e) {
                        throw new BuildException(e, getLocation());
                }
        }


        protected void validate() {
                if (file==null)      throw new BuildException("You must specify a file to read.");
                if (!file.canRead()) throw new BuildException("Can not read file " + file.getAbsolutePath());
                if (section==null)   throw new BuildException("You must specify a section to read.");
        }
       
       
        public void setFile(File file) {
                this.file = file;
        }
        public void setPrefix(String prefix) {
                this.prefix = prefix;
        }
        public void setSection(String section) {
                this.section = section;
        }
       
       
}


<project>
        <taskdef name="loadgroup" classname="LoadGroupTask" classpath="build/classes"/>
        <loadgroup file="data.txt" prefix="data." section="Test2"/>
        <echoproperties prefix="data"/>
</project>


Jan

>-----Urspr√ľngliche Nachricht-----
>Von: Kevin Jackson [mailto:[hidden email]]
>Gesendet: Freitag, 23. Februar 2007 03:31
>An: Ant Users List
>Betreff: Re: Using ant to parse text file
>
>Hi,
>
>If you don't want to write a custom task in java, you could also try
><scriptdef> and use a scripting language of your choice
>
>http://ant.apache.org/manual/OptionalTasks/scriptdef.html
>
>Kev
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [hidden email]
>For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Using ant to parse text file

Rebhan, Gilbert

Hi,

> Example:- If I was to provide the value "Test2", I would like the Ant
> to search for "Test2" in the file, and return the value "blue". If I
> provide the value - "Test3", it should return the value "green"

with less coding, you need =

bsf.jar (Bean Scripting Framwork)
http://jakarta.apache.org/bsf/
jruby.jar
http://jruby.codehaus.org/

available for ant  =


<project name="bla" default="main" basedir=".">

    <property name="scanfor" value="Test2"/>

        <target name="depends">
   
        <script language="ruby">
        <![CDATA[
            File.read('Y:/test/sample.txt').scan(/(#{$scanfor})\s*\n\w+=(\w+)/) {|a,b|
            $project.setProperty "scanresult",b
            }
         ]]>
         </script>
       
        </target>
 
        <target name="main" depends="depends">    
     
        <echo>$${scanresult} ==
              ${scanfor} => ${scanresult}      
        </echo>      
       
        </target>

</project>

depends:
main:
     [echo] ${scanresult} ==
     [echo] Test2 => blue



Regards, Gilbert

-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
Sent: Friday, February 23, 2007 9:02 AM
To: [hidden email]
Subject: AW: Using ant to parse text file

I played a little bit ...

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;


public class LoadGroupTask extends Task {

        String prefix;
        File   file;
        String section;
       
        @Override
        public void execute() throws BuildException {
                validate();
                BufferedReader rdr;
                try {
                        rdr = new BufferedReader(new FileReader(file));
                        boolean needThisSection = false;
                        String line = rdr.readLine();
                        while (line!=null) {
                                line = line.trim();
                                if (line.length()>0) {
                                        if (line.indexOf("=") > 0) {
                                                if (needThisSection) {
                                                        String[] parts = line.split("=");
                                                        String propName  = (prefix==null)   ? parts[0] : prefix + parts[0];
                                                        String propValue = (parts[1]!=null) ? parts[1] : "";
                                                        log("Setting property " + propName + " -> " + propValue);
                                                        getProject().setNewProperty(propName, propValue);
                                                }
                                        } else {
                                                needThisSection = section.equals(line);
                                        }
                                }
                                line = rdr.readLine();
                        }
                        rdr.close();
                } catch (Exception e) {
                        throw new BuildException(e, getLocation());
                }
        }


        protected void validate() {
                if (file==null)      throw new BuildException("You must specify a file to read.");
                if (!file.canRead()) throw new BuildException("Can not read file " + file.getAbsolutePath());
                if (section==null)   throw new BuildException("You must specify a section to read.");
        }
       
       
        public void setFile(File file) {
                this.file = file;
        }
        public void setPrefix(String prefix) {
                this.prefix = prefix;
        }
        public void setSection(String section) {
                this.section = section;
        }
       
       
}


<project>
        <taskdef name="loadgroup" classname="LoadGroupTask" classpath="build/classes"/>
        <loadgroup file="data.txt" prefix="data." section="Test2"/>
        <echoproperties prefix="data"/>
</project>


Jan

>-----Urspr√ľngliche Nachricht-----
>Von: Kevin Jackson [mailto:[hidden email]]
>Gesendet: Freitag, 23. Februar 2007 03:31
>An: Ant Users List
>Betreff: Re: Using ant to parse text file
>
>Hi,
>
>If you don't want to write a custom task in java, you could also try
><scriptdef> and use a scripting language of your choice
>
>http://ant.apache.org/manual/OptionalTasks/scriptdef.html
>
>Kev
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [hidden email]
>For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Loading...