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?
example:
customer,deal,NEWFIELD
200000042,23,200000042-23
200000042,34,200000042-34
200000042,35,200000042-35
200000042,65,200000042-65
You can do this using Ant filterchains, something like this basic example:
Two
<loadfile>
tasks are used to process the header and body of the file, then a simple<echo>
task to write the output. A simplistic regular expression works here as the number of fields in the CSV file is small. Thereplaceregex
uses capturing groups to get the first two fields on the line, then in thereplace
string assembles the required output.If there are several fields, then perhaps a
scriptfilter
in the secondloadfile
would be simper to work with:This one takes the line, splits it and then appends the required fields.
Neither example here would work if your data contains embedded commas.
Would it be simpler to embedd a scripting language like Groovy?
Example
src/file1.csv
target/file1.csv
build.xml