使用Ant从CSV文件的两列连接数据 [英] Concatenate data from two columns of a CSV file using Ant

查看:93
本文介绍了使用Ant从CSV文件的两列连接数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在csv文件中创建一个新列,其中包括与"-"-使用Ant结合的其他两个列的串联?

Is there a way to create a new column in a csv file which includes the concatenation of two other columns joined with a "-" - Using Ant?

示例:

customer,deal,NEWFIELD
200000042,23,200000042-23
200000042,34,200000042-34
200000042,35,200000042-35    
200000042,65,200000042-65

推荐答案

嵌入像Groovy这样的脚本语言会更简单吗?

Would it be simpler to embedd a scripting language like Groovy?

├── build.xml
├── src
│   └── file1.csv
└── target
    └── file1.csv

src/file1.csv

customer,deal
200000042,23
200000042,34
200000042,35
200000042,65

target/file1.csv

customer,deal,customer-deal
200000042,23,200000042-23
200000042,34,200000042-34
200000042,35,200000042-35
200000042,65,200000042-65

build.xml

<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>

这篇关于使用Ant从CSV文件的两列连接数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆