AnsweredAssumed Answered

Using Groovy to remove preceding zero not working as expected

Question asked by faiza.naz365123 on May 30, 2017
Latest reply on May 31, 2017 by 51662476

Hi,

I have a requirement to remove leading zeros from a csv specific column using Groovy script. I am using this groovy but this is not working as expected nor giving any result. Can someone please help me what I am doing wrong.

 

Input CSV:

test-trstevens113@msn.com,Test User,0000100098,8797784047620,trstevenstest@msn.com,,,0,2016-01-18 11:36:32.847,0000123652
test-tstevens@lves.washk12.org,Test2 User,0000100098,87976289823652,tstevens222@lves.washk12.org,,,0,2016-01-18 11:35:45.327,000012352

 

I have to remove leading zeros from 3rd column "0000100098" and make it "100098" before i pass this data to my map.

 

import java.util.Properties;
import java.io.InputStream;
import java.io.BufferedReader;


String DELIMITER = ",";

for( int i = 0; i < dataContext.getDataCount(); i++ ) {

InputStream is = dataContext.getStream(i);
Properties props = dataContext.getProperties(i);

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuffer outData = new StringBuffer();
int lineNum = 0;

while ((line = reader.readLine()) != null) {
// Parse the line into separate columns splitting on the delimiter defined above
String[] columns = line.split(DELIMITER);

// Obtain a column's value by its known position on the line (zero-indexed)
String someColumnValue = columns[2];

columns[2] = someColumnValue.replaceFirst("^0+(?!\$)", "");

//String AccountId = someColumnValue.replaceFirst("^0+(?!\$)", "");
//columns[2] = "Faiza";
//columns[2] = AccountId;

// Add line to output buffer including a line break at the end
outData.append(line);

lineNum++;
}

// Convert the output StringBuffer to an InputStream and store in the dataContext
is = new ByteArrayInputStream(outData.toString().getBytes("UTF-8"));
dataContext.storeStream(is, props);

}

Outcomes