有没有办法在csv文件中创建一个新列,其中包含用"-"
连接的另外两列的串联 - 使用Ant?
示例:
customer,deal,NEWFIELD
200000042,23,200000042-23
200000042,34,200000042-34
200000042,35,200000042-35
200000042,65,200000042-65
答案 0 :(得分:0)
您可以使用Ant filterchains执行此操作,类似于以下基本示例:
<property name="in.file" value="input.txt" />
<property name="out.file" value="output.txt" />
<property name="new.field" value="NEWFIELD" />
<property name="sep.char" value="," />
<loadfile srcfile="${in.file}" property="file.head">
<filterchain>
<headfilter lines="1" />
<striplinebreaks />
</filterchain>
</loadfile>
<loadfile srcfile="${in.file}" property="file.body">
<filterchain>
<headfilter skip="1" />
<tokenfilter>
<replaceregex pattern="^([^${sep.char}]*)${sep.char}([^${sep.char}]*)$"
replace="\1${sep.char}\2${sep.char}\1-\2" />
</tokenfilter>
</filterchain>
</loadfile>
<echo file="${out.file}">${file.head}${sep.char}${new.field}
${file.body}</echo>
两个<loadfile>
任务用于处理文件的标题和正文,然后是一个简单的<echo>
任务来编写输出。这里使用简单的正则表达式,因为CSV文件中的字段数很少。 replaceregex
使用捕获组获取该行的前两个字段,然后在replace
字符串中组合所需的输出。
如果有多个字段,那么第二个loadfile
中的scriptfilter
可能更容易使用:
<loadfile srcfile="${in.file}" property="file.body">
<filterchain>
<headfilter skip="1" />
<scriptfilter language="javascript"><![CDATA[
var line = self.getToken( );
var fields = line.split( "," );
self.setToken( line + "," + fields[0] + "-" + fields[1] );
]]></scriptfilter>
</filterchain>
</loadfile>
这一行接受该行,将其拆分,然后附加必填字段。
如果您的数据包含嵌入的逗号,则此处的示例都不起作用。
答案 1 :(得分:0)
嵌入像Groovy这样的脚本语言会更简单吗?
├── build.xml
├── src
│ └── file1.csv
└── target
└── file1.csv
customer,deal
200000042,23
200000042,34
200000042,35
200000042,65
customer,deal,customer-deal
200000042,23,200000042-23
200000042,34,200000042-34
200000042,35,200000042-35
200000042,65,200000042-65
<project name="demo" default="build">
<available classname="org.codehaus.groovy.ant.Groovy" property="groovy.installed"/>
<target name="build" depends="install-groovy">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<groovy>
ant.mkdir(dir:"target")
new File("target/file1.csv").withWriter {
new File("src/file1.csv").splitEachLine(",") { customer, deal ->
it.println "${customer},${deal},${customer}-${deal}"
}
}
</groovy>
</target>
<target name="install-groovy" description="Install groovy" unless="groovy.installed">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/groovy.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.4.7/groovy-all-2.4.7.jar"/>
<fail message="Groovy has been installed. Run the build again"/>
</target>
</project>