Find / Replace in a file with fixed column

Help with writing and running scripts

Find / Replace in a file with fixed column

Postby odemeusy » Thu Jul 12, 2007 3:08 pm

Hi

I am new to Scripting in UltraEdit. Here my problem: I have a huge file where I need to change some formating. As an example, consider the following 2 records (all the records are aligned in columns):

200701277 2007060105191000009781400052448 380J00 1- 1350- 1350-USD 04 000001ZG2 R421 0000000000000010097863321

200701273 2007060110356800009780307275004 29255M 9 3145 3145 USD 01 000001ZL2 R421 0000000000000020097863403

What I want/need is :
1) wherever a minus (-) move it in front of the number
2) add a decimal separator for the last two amounts

The result for the 2 records should be

200701277 2007060105191000009781400052448 380J00 -1 -13.50 -13.50USD 04 000001ZG2 R421 0000000000000010097863321

200701273 2007060110356800009780307275004 29255M 9 31.45 31.45USD 01 000001ZL2 R421 0000000000000020097863403

Can someone help?

Thanks a lot
User avatar
odemeusy
Newbie
 
Posts: 2
Joined: Mon Apr 02, 2007 11:00 pm

Re: Find / Replace in a file with fixed column

Postby jorrasdk » Thu Jul 12, 2007 8:59 pm

Ok, since it is a file with data that are column aligned, there are really a lot you can do with straight forward column mode operations. See power tip for column mode...

Ok, but you asked for a script, so here goes :-) - but beware "...huge file..." and scripts sometimes = slow.

You didn't use [code ] [/code ] tags :-( to wrap round your example, but from "view source" I was able to reconstruct it:

Code: Select all
200701277  2007060105191000009781400052448     380J00             1-               1350-               1350-USD  04 000001ZG2 R421  0000000000000010097863321
200701273  2007060110356800009780307275004     29255M             9                3145                3145 USD  01 000001ZL2 R421  0000000000000020097863403


Then run this (hopefully self explaining) script demonstrating the powerful column mode and som simple regex:

Code: Select all
UltraEdit.ueReOn(); /* UE style regex engine */
UltraEdit.selectClipboard(9); /* use clipboard 9 */
UltraEdit.columnModeOn(); /* switch to column mode */

wDoc = UltraEdit.document[getActiveDocumentIndex()]; /* I like shortcuts ;-) */

wDoc.gotoLine(1,54); /* goto start of the area we are going to reformat */
wDoc.columnCut(55); /* cut the whole area to clipboard - 55 chars wide */

/* ------------- */
UltraEdit.newFile(); /* open a blank file */
tempDoc = UltraEdit.document[getActiveDocumentIndex()]; /* I like shortcuts ;-) */
tempDoc.paste();

tempDoc.findReplace.replaceAll=true;
tempDoc.findReplace.regExp=true;
tempDoc.findReplace.replace("^([0-9]+^)^([ -]^)","^2^1"); /* move sign in front of number */

tempDoc.top();
tempDoc.selectToBottom(); /* select reformatted text */
tempDoc.copy();
UltraEdit.closeFile(tempDoc.path,2); /* we are finished with this temporary file */
/* ------------- */

wDoc.paste(); /* and we're back */
UltraEdit.clearClipboard(); /* clean up */
UltraEdit.selectClipboard(0); /* switch to primary clipboard */

wDoc.gotoLine(1,69); /* 1st column to amputate to make room for decimal separator */
wDoc.columnDelete(1);
wDoc.gotoLine(1,86); /* 1st column for decimal separator */
wDoc.columnInsert(".");

wDoc.gotoLine(1,89); /* 2nd column to amputate to make room for decimal separator */
wDoc.columnDelete(1);
wDoc.gotoLine(1,106); /* 1st column for decimal separator */
wDoc.columnInsert(".");

/* ------------- */
/* Find the tab index of the active document */
function getActiveDocumentIndex() {
   var tabindex = -1; /* start value */

   for (i = 0; i < UltraEdit.document.length; i++)
   {
      if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
         tabindex = i;
         break;
      }
   }
   return tabindex;
}


to produce this result:
Code: Select all
200701277  2007060105191000009781400052448     380J00             -1              -13.50              -13.50USD  04 000001ZG2 R421  0000000000000010097863321
200701273  2007060110356800009780307275004     29255M              9               31.45               31.45USD  01 000001ZL2 R421  0000000000000020097863403
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Find / Replace in a file with fixed column

Postby odemeusy » Fri Jul 13, 2007 6:38 am

Wow ! Thanks a lot! 8)
This is exactly what I needed. It did the job perfectly.
On top of that, I have learned a lot about Columns (Colomn Mode) and Script.
Thanks very much for being that helpful and responsive in such a short time.
:D
User avatar
odemeusy
Newbie
 
Posts: 2
Joined: Mon Apr 02, 2007 11:00 pm


Return to Scripts