DanutClapa
DanutClapa

Reputation: 642

delete all lines from text except first using ANT

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.

Upvotes: 0

Views: 118

Answers (1)

Chad Nouis
Chad Nouis

Reputation: 7041

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

Upvotes: 2

Related Questions