Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Connect 2026 Agenda Now Available: Explore Sessions
cancel
Showing results for 
Search instead for 
Did you mean: 
VijayTalend
Contributor
Contributor

cProcessor java code for adding character at end of specific line

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

Labels (5)
1 Solution

Accepted Solutions
Anonymous
Not applicable

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

View solution in original post

3 Replies
Anonymous
Not applicable

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

VijayTalend
Contributor
Contributor
Author

Thanks for the Java code. It worked like a charm 0683p000009MACn.png

Anonymous
Not applicable

Not a problem, I'm glad it helped 🙂