Apache Ant has lots of useful built-in tasks. However, a task for merging text files is not among them. Fortunately, we can combine some built-in tasks to achieve this goal.
Below, you’ll find the macro along a simple demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <project default="merge-demo"> <target name="merge-demo"> <merge tofile="merged.txt"> <sourcefiles> <fileset dir="inputfiles"></fileset> </sourcefiles> </merge> </target> <!-- = = = = = = = = = = = = = = = = = macrodef: merge = = = = = = = = = = = = = = = = = --> <macrodef name="merge"> <attribute name="tofile"></attribute> <element name="sourcefiles"></element> <sequential> <tempfile property="temp.file" prefix="ant.merge." deleteonexit="true"></tempfile> <concat destfile="${temp.file}" fixlastline="true"> <sourcefiles></sourcefiles> </concat> <copy file="${temp.file}" tofile="@{tofile}"> <filterchain> <tokenfilter> <linetokenizer></linetokenizer> </tokenfilter> <sortfilter></sortfilter> <uniqfilter></uniqfilter> </filterchain> </copy> </sequential> </macrodef> </project> |
Remarks and Caveats
- Contents of all files are sorted by line.
- Duplicate lines are removed.
Example
The example runs on the following directory:
with the following file contents:
file1.txt
# random prefix file line remark 7609 file1.txt 1 24307 file1.txt 2 31889 file1.txt 3 no newline afterwards
file2.txt
# random prefix file line remark 5808 file2.txt 1 32706 file2.txt 2 preceding empty line 7848 file2.txt 3 following empty line 17727 file2.txt 4 3071 file2.txt 5 one newline afterwards
file3.txt
# random prefix file line remark 16411 file3.txt 1 10605 file3.txt 2 followed by several newlines
Once we run the Ant script, the resulting file will look as follows:
merged.txt
# random prefix file line remark 10605 file3.txt 2 followed by several newlines 16411 file3.txt 1 17727 file2.txt 4 24307 file1.txt 2 3071 file2.txt 5 one newline afterwards 31889 file1.txt 3 no newline afterwards 32706 file2.txt 2 preceding empty line 5808 file2.txt 1 7609 file1.txt 1 7848 file2.txt 3 following empty line