从文本中删除除首先使用 ANT 之外的所有行

delete all lines from text except first using ANT

有没有办法使用 ANT 删除文本文件中除第一行以外的所有文本? 示例:

First line
Second line
.....
Line n

我想删除除第一行以外的所有内容。这些行可以包含任何文本。

使用<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 在 运行 Ant

之前
First line
Second line
.....
Line n

test.txt 在 运行 蚂蚁

之后
First line