Ant equivalent of cut | sort | uniq

2019-05-25 08:11发布

In an Ant task I set a property which is a list of files. e.g.

web/src/main/test/com/whatever/Ralph
business/src/main/test/com/whatever/Alice
web/src/main/test/com/whatever/Bob

I would like to extract the set of subdirectories from this list. In bash I'd:

$ cat filename | cut -d'/' -f1 | sort | uniq
business
web

Is there a way I can do something similar in an Ant macro? It needs to run on Windows too, so <exec> is not an option.

标签: ant
1条回答
狗以群分
2楼-- · 2019-05-25 08:30

You can do this using a loadresource task with a filterchain. Something like this perhaps:

<property name="list.of.files">
web/src/main/test/com/whatever/Ralph
business/src/main/test/com/whatever/Alice
web/src/main/test/com/whatever/Bob
</property>

<loadresource property="dirs">
    <string value="${list.of.files}" />
    <filterchain>
        <replaceregex pattern="/.*" replace="" />
        <sortfilter />
        <uniqfilter />
    </filterchain>
</loadresource>

<echo message="${dirs}" />

Result:

 [echo] business
 [echo] web

BUILD SUCCESSFUL

In older versions of Ant (<1.7) you could do the same by writing the property out to a file, then using a loadfile task with filterchain.

查看更多
登录 后发表回答