Need to insert a string every 1000 lines

Help with writing and running scripts

Need to insert a string every 1000 lines

Postby Programmergeek » Wed Feb 11, 2009 4:04 pm

Sorry if this is in the wrong spot I don't know if this should be a macro, script, etc. I am new to UltraEdit.

Anyhow I have a bunch of SQL code and I need to insert a "GO" every 1000 lines to the end of the file how would I go about this? I have 6 files about 200,000 rows each.
Programmergeek
Newbie
 
Posts: 2
Joined: Wed Feb 11, 2009 3:57 pm

Re: Need to insert a string every 1000 lines

Postby pietzcker » Thu Feb 12, 2009 2:25 am

I'm assuming you have UE V14. In theory, this Perl regex should work. In practice, this might depend on the size of the file and the length of the lines. Try the following on a backup copy first:

Make sure the cursor is at the top of the file.
Open the Replace dialog, select Perl regular expressions and enter

((?:.*\r\n){1000})

in the search box and

\1GO\r\n

in the replace box. Then click on "Replace all".

If that doesn't work, you could also use a macro that simply simulates a "Down Arrow" keystroke 1000 times and then enters "GO<enter>", looping until the end of file. But that will be a lot slower, especially if you have to add checks that you haven't yet reached EOF. So try the above regex first.
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Need to insert a string every 1000 lines

Postby Programmergeek » Tue Feb 17, 2009 1:17 pm

I couldn't get the regex replace to work. Maybe I was just not doing it right. However, I started to write a JS. Here is what I have. It works, but I have to figure out how to tell how may lines the current document has.

Code: Select all
UltraEdit.activeDocument.top();

// current document length set to 27 need to set it to a proper function for length. Or do I need it at all???
var num = 27;
for (lineNum = 10; lineNum < num; lineNum += 1000)
{
   UltraEdit.activeDocument.gotoLine(lineNum);
//UltraEdit.activeDocument.top();
UltraEdit.activeDocument.write("GO\r\n");
}

UltraEdit.outputWindow.write("Script Compleated")
Programmergeek
Newbie
 
Posts: 2
Joined: Wed Feb 11, 2009 3:57 pm

Re: Need to insert a string every 1000 lines

Postby Mofi » Sat Feb 21, 2009 7:54 am

What about following script:

Code: Select all
UltraEdit.activeDocument.bottom();
/* Verify if the last line of the copied text also has a line ending.
   If this is not the case insert one and make sure that the auto
   indent feature has not added additional preceding white-spaces. */
if (UltraEdit.activeDocument.isColNumGt(1)) {
   UltraEdit.activeDocument.insertLine();
   if (UltraEdit.activeDocument.isColNumGt(1)) {
      UltraEdit.activeDocument.deleteToStartOfLine();
   }
}
var nInsertCount = Math.floor(UltraEdit.activeDocument.currentLineNum / 1000);
var nActLineNumber = 0;
while (nInsertCount--) {
   nActLineNumber += 1001;
   UltraEdit.activeDocument.gotoLine(nActLineNumber,1);
   UltraEdit.activeDocument.write("GO\r\n");
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4042
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts