on ‎2020 Jul 20 9:12 AM
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
Request clarification before answering.
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 messageRegards,
Morten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 🙂
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 6 | |
| 6 | |
| 6 | |
| 5 | |
| 4 | |
| 3 | |
| 3 | |
| 3 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.