OL Learn

Adding delimiter in preprocessor script

Hi,
I have a tab delimited file in which the detail lines (always starting with ’02’) have one delimiter less than the header lines.
I need help with a script in preprocessor either to count the delimiters and add one at the end of each line missing it, or just adding a tab delimiter at the end of each line which starts with ’02’.

Any help is much appreciated!

thanks
Akis

I think I did it, it seems to work fine.
(hope it doesn’t become a habit to respond to myself :slight_smile: )

**var** tmpFile = createTmpFile();
**var** originalFile = openTextReader(data.filename);
**var** modifiedFile = openTextWriter(tmpFile.getPath());

**while** ((line = originalFile.readLine())!= **null** ){

**if** (line.startsWith("02")== **true** ) {
modifiedFile.write(line + "\t" + "\n");

} **else** {

modifiedFile.write(line + "\n");
}

}

modifiedFile.close();
originalFile.close();
deleteFile(data.filename);
tmpFile.move(data.filename);

On the contrary, keep answering yourself, it makes our jobs so much easier that way! :stuck_out_tongue:

That said, allow me to suggest a minor improvement to the content of your loop:

while ((line = originalFile.readLine())!=null ){
   if(line.startsWith("02")) line += "\t";
   modifiedFile.write(line + "\n");
}

It’s the same code, it’s just slightly cleaner.
Of course, you can make it tighter - and more cryptic! - with a single line of code inside the loop:

   modifiedFile.write(line + (line.startsWith("02") ? "\t\n":"\n"));

But that just makes it harder to read.

1 Like