on 12-27-2017 7:58 AM
Hi Experts,
I have a scenario where the Sender is SFTP. I need to read the value of a filename.
Now i able read full filename by using CamelFileName in Content Modifier.
Filename:
ABC_20171215_46270.51_API1_hhmmss.txt
I want to extract only API1, after third underscore read the data.
Anyone can please tell me, how can be we do this.
Regards,
Thouheed.
Hi Mohammad
When you have the filename, all that's left is a bit of string handling.
The java.lang.String class has a lastIndexOf method, that you can use to find the index of the last occurrence a given character. There's an overloaded version that takes an index to begin searching from. Using those two methods, you can find the index of the last and the second to last underscore, and then use the substring method to extract your target string. Here's some Groovy code that does exactly that:
def fileName = 'ABC_20171215_46270.51_API1_hhmmss.txt'
def lastUnderscoreIndex = fileName.lastIndexOf('_')
println "lastUnderScoreIndex = ${lastUnderscoreIndex}"
def secondToLastUnderscoreIndex = fileName.lastIndexOf('_', lastUnderscoreIndex - 1)
println "secondToLastUnderscoreIndex = ${secondToLastUnderscoreIndex}"
def targetString = fileName.substring(secondToLastUnderscoreIndex + 1, lastUnderscoreIndex)
println "targetString = ${targetString}"
Regards,
Morten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Morten,
We have a input data like below in that we need to read first to second dot(.) data.
abc_31012018_1000.00_123_xyz_20180131072155.txt Processing Failed with error code 36005 and with reason Multi currency transactions not allowed.Error in record number 1.
I have to read second dot to end of data like below.
txt Processing Failed with error code 36005 and with reason Multi currency transactions not allowed.Error in record number 1.
how to archive it in groovy script.
Regards,
Thouheed.
Hello Touheed,
Looks like this question is not related to above mentioned query.If its not related please create a new post.
And it would be always great if you can up vote the helpful answers or accept the answers which are helped you before marking the question to closure .
Regards,
Sriprasad Shivaram Bhat
Thanks Morten,
Same groovy script i have used, but some changes has been done now it's working fine.
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
def secondToLastUnderscoreIndex = fileName.lastIndexOf('_', lastUnderscoreIndex - 1)
println "secondToLastUnderscoreIndex = ${secondToLastUnderscoreIndex}"
def targetString = fileName.substring(secondToLastUnderscoreIndex + 1, lastUnderscoreIndex)
println "targetString = ${targetString}"*/
def body = message.getBody(java.lang.String) as String;
def lastUnderscoreIndex = body.lastIndexOf('_');
def secondToLastUnderscoreIndex = body.lastIndexOf('_', lastUnderscoreIndex - 1);
def targetString = body.substring(secondToLastUnderscoreIndex + 1, lastUnderscoreIndex);
message.setBody(targetString.toString());
return message;
}
Thanks and Regrads,
Md.Thouheed
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Oh, and you don't need the println statements. That was just to show the data, when running from the command line.
User | Count |
---|---|
90 | |
10 | |
10 | |
10 | |
7 | |
7 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.