cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Remove first few lines in csv to xml convertor

former_member229127
Participant
0 Likes
2,346

Hi all,

am trying to exclude the first five lines from csv file but it is not happening because of it the csv to xml converter is not working.

Script which i have written to exclude the first five lines:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message)
{
def body = message.getBody(String.class);
body = body.substring(body.indexOf('\n')+5);
message.setBody(body);
return message;
}

CSV file from the source:

Regards,

Sandhya

Accepted Solutions (1)

Accepted Solutions (1)

MortenWittrock
SAP Mentor
SAP Mentor

Hi Sandhya

If you know that the line separator is \n, the following will work:

def body = message.getBody(String)
message.setBody(body.split('\n').drop(5).join('\n'))
return message

Regards,

Morten

mathias_rohland
Explorer

Thanks for taking me onto the right track Morten. I needed to subsequently get a fixed number of lines from a larger CSV using a Looping Process Call in CIS where this clever approach didn't work for me.

String belnrList = """905247227
905247199
905244621
905241898
905241873
905239105
905233503
905223135""";
String eol = "\n";
int maxLoops = 99;
int loopCount = 1;

while ((! belnrList.isEmpty()) && (loopCount < maxLoops)) {
   String[] belnrChunk = belnrList.split(eol).take(3);
   String[] belnrListRemainder = belnrList.split(eol).drop(3);  
   StringBuilder stringBuilder = new StringBuilder();
   for (int i = 0; i < belnrListRemainder.length; i++) {
      stringBuilder.append(belnrListRemainder[i]).append(eol);
   }
   belnrList = stringBuilder.toString();
   // See the current split result
   println("belnrChunk - " + belnrChunk + " - loopCount " + loopCount.toString());
   loopCount++;
}

To get the remainder back as carriage return separated String I used a StringBuilder which worked fine for me. Another possibility to compose this String would be to use Streams so something like ...

String result = Stream.of(arr).collect(Collectors.joining("\n"));

... should work too ðŸ™‚

Answers (1)

Answers (1)

Hi sandhya.ravi,

Could you use groovy to read lines and skip ones starting with *.

You could find a sample code in below link.

https://answers.sap.com/questions/13005450/to-read-first-line-of-flat-file-and-return-whole-f.html

hope this helps !

thanks and regards,

Praveen T