Is there a way to delete all the text from a text file using ANT except the first line?
Example:
First line
Second line
.....
Line n
I want to delete everything except first line. The lines can contain any text.
Use <loadfile> with a <headfilter> to get just the first line of a file. Then, the original file can be overwritten using the file attribute of <echo>.
<!-- Use headfilter to save the file's first line in a property. -->
<loadfile srcfile="${text.file}" property="first.line">
<filterchain>
<headfilter lines="1"/>
</filterchain>
</loadfile>
<!-- Overwrite the file with only the first line. -->
<echo file="${text.file}">${first.line}</echo>
test.txt before running Ant
First line
Second line
.....
Line n
test.txt after running Ant
First line
Related
I want to convert multiple file formats to a single file format.
Example: D:\myrepo\rough has 3 files
1. abc.sql
2. def.xml
3. ghi.dmp
I want them all to be converted to .txt using glob mappers.
<?xml version ="1.0"?>
<project name = "roughone" default="taget1">
<target name= "target1">
<move todir="D:\myrepo\rough">
<fileset dir="D:\myrepo\rough">
</fileset>
<mapper type ="glob" from="*" to="*.txt"/>
</move>
</target>
</project>
This is giving
1. abc.sql.txt
2. def.xml.txt
3. ghi.dmp.txt where as i need only abc.txt,def.txt and ghi.txt.
Plz let me know how this can be fixed(from= "." is not helping too).
Replace your globmapper with the following <regexpmapper>:
<regexpmapper from="^(.*)[.][^.]+$$" to="\1.txt"/>
The above regular expression captures the part of each file name before the final period. The regular expression also discards whatever extension the file previously had.
The double "$$" is needed because Ant would interpret a single "$" as the beginning of a property reference.
I've looked all over the net as to how I can load a list of files that contain spaces and don't yet exist with an Ant task.
I have a file that contains one file path per line, like so:
dir1/dir2/dir with spaces/file1.js
dir1/dir2/dir with spaces/dir3/file2.js
dir1/file1.js
Since the paths have spaces I cannot use:
<filelist files="..." />
The files also don't exist yet, so it seems like I can't use
<fileset>
<includesfile name="..." />
</fileset>
Any ideas would be greatly appreaciated.
You can use a resourcelist for this. For example, if your list of files are in a file called 'files.txt':
<resourcelist id="files">
<file file="files.txt"/>
</resourcelist>
<touch mkdirs="true">
<resources refid="files" />
</touch>
For me this yields:
[touch] Creating .../filelist/dir1/dir2/dir with spaces/file1.js
[touch] Creating .../filelist/dir1/dir2/dir with spaces/dir3/file2.js
[touch] Creating .../filelist/dir1/file1.js
The reason this works is that a <resourcelist> treats each line in the file read as a separate resource, so line separators rather than commas or spaces divide the items.
So i have an ant build file which needs to modify this other file named modify.properties. In this properties file i have a line such as this -
CSDT_FLAG_CSELEMENT_FWUIDS=CSElement:4f826165-4744-4d78-63f0-4f437eb17872,b2db8c3d-ec81-4c6e-9425-ff0860f4511b,13f5a17a-8dac-443b-bcf8-b1675e660c53
In my build file I will search for the term CSElement and get the line that contains this term
<loadfile
property="csdtFlagPropertiesFile"
srcFile="../cms-distribution/src/main/x/csdtflags.properties">
<filterchain>
<filterreader classname="org.apache.tools.ant.filters.LineContainsRegExp">
<param type="regexp" value="CSElement"/>
</filterreader>
</filterchain>
</loadfile>
Now i have the line CSDT_FLAG_CSELEMENT_FWUIDS=CSElement:4f826165-4744-4d78-63f0-4f437eb17872,b2db8c3d-ec81-4c6e-9425-ff0860f4511b,13f5a17a-8dac-443b-bcf8-b1675e660c53 and i can replace the line using the containsregex method in filterchain however i am unable to write this back onto the file. A message such as echo message will rewrite the entire file but i just want to replace that line with another line. replacing works but this saves it into a property and i dont know how i can rewrite this on the file. Any help much appreciated.
You could use the ReplaceRegExp task to make a substitution in the file.
My problem is, I have to read the source path for a copy job from an xml file and then copy all files in that dir read from the xml file to another dir.
Since code is more than words:
<xmltask source="${projectfile}">
<copy path="Project/RecentResultsInfo/ResultsDirectoryOfRecentLoadTest/text()" property="recentdir" attrValue="true"/>
</xmltask>
<copy todir="${targetdirectory}">
<fileset dir="${recentdir}"/>
</copy>
The output when running this target is:
C:\develop\build.xml:44: Warning: Could not find resource file "C:\develop\C:\Programme\tool\test_90\" to copy.
It seems in fileset it does not recognize, that recentdir holds a full path inside. The written xml from the application has a newline before and after the path in the xml file that is read with the path. So ant does not recognize the path since theres a newline in front of it.
Is there anything like trim for ant?
Can anybody help me getting ant to accept that path?
Done it now by using Ant-Contrib, but that is used in this project anyway.
<xmltask source="${projectfile}">
<copy path="Project/RecentResultsInfo/ResultsDirectoryOfRecentLoadTest/text()" property="recentdirraw" attrValue="true"/>
</xmltask>
<!-- replace newlines and whitespace from read path -->
<propertyregex property="recentdir" input="${recentdirraw}" regexp="^[ \t\n]+|[ \t\n]+$" replace="" casesensitive="false" />
<copy todir="${targetdirectory}">
<fileset dir="${recentdir}"/>
</copy>
Simply modifying the property with a regex trimming the text by striping of whitespace and newlines.
As far as I can see, the copy element in xmltask provides a trim attribute.
trims leading/trailing spaces when writing to properties
Does that work?
Given an ant fileset, I need to perform some sed-like manipulations on it, condense it to a multi-line string (with effectively one line per file), and output the result to a text file.
What ant task am I looking for?
The Ant script task allows you to implement a task in a scripting language. If you have JDK 1.6 installed, Ant can execute JavaScript without needing any additional dependent libraries. The JavaScript code can read a fileset, transform the file names, and write them to a file.
<fileset id="jars" dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
<target name="init">
<script language="javascript"><![CDATA[
var out = new java.io.PrintWriter(new java.io.FileWriter('jars.txt'));
var iJar = project.getReference('jars').iterator();
while (iJar.hasNext()) {
var jar = new String(iJar.next());
out.println(jar);
}
out.close();
]]></script>
</target>
Try the ReplaceRegExp optional task.
ReplaceRegExp is a directory based task for replacing the occurrence of a given regular expression with a substitution pattern in a selected file or set of files.
There are a few examples near the bottom of the page to get you started.
Looks like you need a conbination of tasks:
This strips the '\r' and '\n' characters of a file and load it to a propertie:
<loadfile srcfile="${src.file}" property="${src.file.contents}">
<filterchain>
<filterreader classname="org.apache.tools.ant.filters.StripLineBreaks"/>
</filterchain>
</loadfile>
After loading the files concatenate them to another one:
<concat destfile="final.txt">
...
</concat>
Inside concat use a propertyset to reference the files content:
<propertyset id="properties-starting-with-bar">
<propertyref prefix="src.file"/>
</propertyset>
rodrigoap's answer is enough to build a pure ant solution, but it's not clean enough for me and would be some very complicated ant code, so I used a different method: I subclassed ant's echo task to make an echofileset task, which takes a fileset and a mapper. Subclassing echo buys me the ability to output to a file. A regexmapper performs the transformation on filenames that I need. I hardcoded it to print out each file on a separate line, but if I needed more flexibility I could add an optional separator attribute. I also thought about providing the ability to output to a property, too, but it turned out I didn't need it since I echo'ed straight to a file.