Removing a line of a file

Hi!

I want to remove the whole line of a flat file if it begins with a certain combination of characters and a certain substring is empty. My flat file structure is below…

 
A20A  47338000000005          
A20P  47338000000005              1831    
A20O  47338000000005            Air Solutions Brussel  
A20N  47338000000005                         080293        
A20M  47338000000005    
A20I  47338000000005    

What I need to do is eliminate the lines which contain no real data. The Codes and id numbers on the left identify a record, but the real data is in fixed position format to the right. For instance, the A20A, A20M and A20I are all blank… so I need to erase them. I was thinking of writing a service to capture the Code, and the substring where data is SUPPOSED to be and determining whether to delete it then… but how can I delete a line of a file ?

TIA,
Jessica

Hello Jessica,
THe way I would solve this is to write a simple Java service to subString() each line of the flat file at the position where real data should be. (You can also do this within webMethods by using webMethods service to read in the flat file and using while loop to look at each line) but for me it is more direct in Java to do this.
I notice that you also have three distinct “fields” here so you could also use the Tokenize string method in webMethods string services to put a place holder between each of these fields and then map them to distinct fields and determine if the last on is null.

Can you give me the pattern for a java service. I’ve never written one before. I know java, but how does webMethods deal with variables etc?

hi,

i deal with such problem pretty often, and regex really works well for such problem.

chk out the gnu.regexp package, it’s an open source Regex impl for JAVA.

HTH.

chiew

One way you can do this is to use a string tokenizer on the document. Use a carriage return for the delimiter.

pub.string:tokenizer

TO do this, set the delimiter variable to carriage return by opening up the variable in a large window and hitting the enter key once. Then, close the window with your mouse. Do not hit the enter key as it sometimes adds carriage returns.

The stringTokenizer will return an array of your file, each item in the array is a string.

Create a loop and use the array as the in-array.

Create a second string list in a map step right before the loop.

As you iterate through the loop, each line will now be a single text item that you can pass to a substring, or use regex (with a branch statement) or any number of ways. Perhaps the easiest that I can think of off the top of my head is to either to pub.string:length which will return the length of the string. If it equals the line qualifer and nothing else, then go to the next iteration, otherwise, then the line is good and append the text field with the line in it to the string list you create above the loop.

Do that with appendToStringList.

HTH

Ray

Ray,

Thanks so much! Simple yet brilliant! It works perfectly.

Many Thanks,
Jessica