Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi,
I am working on a talend map. I am using cProcessor to change the data received.
The example file is like shown below. My requirement is if the line starts with PR then append XYZ at the end of line.
Input file
PA;AB;2020;1;2
PR;DD;2020;3;4
PB;AB;2020;5;6
PR;DD;2020;7;2
Output file
PA;AB;2020;1;2
PR;DD;2020;3;4XYZ
PB;AB;2020;5;6
PR;DD;2020;7;2XYZ
Can anyone please let me know how we can achieve it using cProcessor.
I am reading whole file by below java code -
String body = exchange.getIn().getBody(String.class);
How to add XYZ where line starts with PR in string body ?
Regards
This is a quick bit of code I have put together starting from after you have retrieved your body message.
String lines[] = body.split("(\r\n|\n)");
String outputBody = "";
for(int i=0; i<lines.length;i++){
if(lines[i].startsWith("PR")){
outputBody = outputBody+lines[i]+"XYZ\r\n";
}else{
outputBody = outputBody+lines[i]+"\r\n";
}
}
System.out.println(outputBody);
I've assumed that you data has a carriage return and line feed (\r\n) separating the rows. You may need to test and tweak this. All you need to do after this is to add it back to the In Message of the Exchange.
Let me know how it goes
This is a quick bit of code I have put together starting from after you have retrieved your body message.
String lines[] = body.split("(\r\n|\n)");
String outputBody = "";
for(int i=0; i<lines.length;i++){
if(lines[i].startsWith("PR")){
outputBody = outputBody+lines[i]+"XYZ\r\n";
}else{
outputBody = outputBody+lines[i]+"\r\n";
}
}
System.out.println(outputBody);
I've assumed that you data has a carriage return and line feed (\r\n) separating the rows. You may need to test and tweak this. All you need to do after this is to add it back to the In Message of the Exchange.
Let me know how it goes
Thanks for the Java code. It worked like a charm
Not a problem, I'm glad it helped 🙂