过滤文件就地使用Ant? [英] Filtering Files In-Place with Ant?
问题描述
我有我想为其做就地采用Apache Ant(在Linux 1.7.1版本)字符串过滤文件的目录。
I have a directory of files for which I'd like to do "in-place" string filtering using Apache Ant (version 1.7.1 on Linux).
例如,假设在目录 MYDIR
我有文件富
,栏
和巴兹
。进一步假设,经常前pression OLD出现的所有([0-9])
应改为新建\\ 1
,例如: OLD2
→ NEW2
。 (请注意,替换
Ant任务将无法工作,因为它不支持常规的前pression过滤。)
For example, suppose that in directory mydir
I have files foo
, bar
, and baz
. Further suppose that all occurences of the regular expression OLD([0-9])
should be changed to NEW\1
, e.g. OLD2
→ NEW2
. (Note that the replace
Ant task won't work because it does not support regular expression filtering.)
此测试的情况可以用下面的命令猛砸创建(蚂蚁会在当前目录下运行,即 MYDIR
的父目录):
This test situation can be created with the following Bash commands (ant will be run in the current directory, i.e. mydir
's parent directory):
mkdir mydir
for FILE in foo bar baz ; do echo "A OLD1 B OLD2 C OLD3" > mydir/${FILE} ; done
这是我第一次做过滤用Ant的尝试:
Here is my first attempt to do the filtering with Ant:
<?xml version="1.0"?>
<project name="filter" default="filter">
<target name="filter">
<move todir="mydir">
<fileset dir="mydir"/>
<filterchain>
<tokenfilter>
<replaceregex pattern="OLD([0-9])" replace="NEW\1" flags="g"/>
</tokenfilter>
</filterchain>
</move>
</target>
</project>
运行此第一Ant脚本在 MYDIR
上的文件没有任何影响。在覆盖
参数真正
默认情况下使用移动
蚂蚁任务。我甚至用粒度
设置摆弄,但没有帮助。
Running this first Ant script has no effect on the files in mydir
. The overwrite
parameter is true
by default with the move
Ant task. I even fiddled with the granularity
setting, but that didn't help.
下面是我的第二次尝试,其中作品,但因为临时文件创建有点烦。这个版本的内容移动到文件以过滤
后缀正确过滤内容,然后过滤内容与原来的文件名迁回
Here's my second attempt, which "works," but is slightly annoying because of temporary file creation. This version filters the content properly by moving the content to files with a filtered
suffix, then the filtered content is "moved back" with original filenames:
<?xml version="1.0"?>
<project name="filter" default="filter">
<target name="filter">
<move todir="mydir">
<globmapper from="*" to="*.filtered"/>
<fileset dir="mydir"/>
<filterchain>
<tokenfilter>
<replaceregex pattern="OLD([0-9])" replace="NEW\1" flags="g"/>
</tokenfilter>
</filterchain>
</move>
<move todir="mydir">
<globmapper from="*.filtered" to="*"/>
<fileset dir="mydir"/>
</move>
</target>
</project>
能在第一次尝试(不含临时文件)进行合作?
推荐答案
查看取代任务:
<replace
dir="mydir"
includes="foo, bar, baz">
<replacefilter token="OLD" value="NEW" />
</replace>
或 replaceregexp 任务:
<replaceregexp
file="${src}/build.properties"
match="OldProperty=(.*)"
replace="NewProperty=\1"
byline="true"/>
这篇关于过滤文件就地使用Ant?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!