Paweł Green
Paweł Green

Reputation: 15

add semicolon on the end of the first line in csv file -groovy

I would like to ask how can i add " ; " at the end of the first line of the following csv file:

file looks like this:

> field1,field2,field3,fieldN(here i would like to have a static semicolon [;]) 
> value1,value2,value3,valueN N rows....

so the payload at the end looks like:

 > field1,field2,field3,fieldN;
 > value1,value2,value3,valueN

Simple groovy code i tried (but it adds semicolon at the end of the file not at the end of the first line):

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message) {

//get payload and def string to append
        
    Map headerMap = message.getHeaders();
    def payload = message.getBody(java.lang.String);
    def a = ";";
    
 
 //append character and set as payload
    payload = payload + a;
    message.setBody(payload);
    
 //return   
    
 return message;
 }

Upvotes: 1

Views: 470

Answers (1)

cfrick
cfrick

Reputation: 37033

Since you are already have the whole body loaded the impact of just replacing the first line-break with your character (and a line-break) should be a good compromise. E.g.

def data = """a,b,c
d,e,f
g,h,i"""

assert data.replaceFirst("\n", ";\n") == "a,b,c;\nd,e,f\ng,h,i"
//          ^^^^^^^^^^^^

Upvotes: 2

Related Questions