Replacement with loop...

Help with writing and running scripts

Replacement with loop...

Postby rick_l » Thu Oct 11, 2007 4:16 am

Greetings!

I'm a new user and I have not much experience in programming. What I want to do is a simple job but I haven't been able to figure it out on my own - it's probably a very easy thing for most users...

Here is the file that I have;

x323,x234,x932,x991
x234,x323,x991
x139,x234
..

What I want to do is the following:

for each line (there are about 400000 lines in this file), get the line number
find the first word in each line
replace all instances of this word in this entire file with the line number
then go to the next line, repeat till the end of file

Basically I am trying to replace all these names with serial numbers (same as the line number)

Here is the code that I wrote;

=================
var index;
for (index = 1; index < 4000; index++) {
UltraEdit.activeDocument.gotoLine(index,1);
UltraEdit.activeDocument.selectWord;
var text = UltraEdit.activeDocument.selection;
UltraEdit.activeDocument.findReplace.replace(text,index);
}

============

I guess I messed up "text" and "index"; but I still can't figure out how to make it work :(

Thanks a lot!

Rick
User avatar
rick_l
Newbie
 
Posts: 2
Joined: Wed Oct 10, 2007 11:00 pm

Re: Replacement with loop...

Postby Mofi » Fri Oct 12, 2007 7:48 am

Here is the script which should work for you. I have tested it on your small example with UE v13.10a+2.

Code: Select all
// Define working environment.
UltraEdit.insertMode();
UltraEdit.columnModeOff();
UltraEdit.activeDocument.hexOff();
UltraEdit.ueReOn();

// Make sure last line of the file has a line termination.
UltraEdit.activeDocument.bottom();
if (UltraEdit.activeDocument.isColNumGt(1)) UltraEdit.activeDocument.insertLine();
UltraEdit.activeDocument.top();

// Define needed variables.
var selword;  // String variable to hold the first word on a line.
var linenr;   // Value variable to hold the line nummber of the current line.

// Define the needed search and replace properties.
UltraEdit.activeDocument.findReplace.matchWord=true;
UltraEdit.activeDocument.findReplace.replaceAll=true;
UltraEdit.activeDocument.findReplace.searchDown=true;
UltraEdit.activeDocument.findReplace.matchCase=false
UltraEdit.activeDocument.findReplace.preserveCase=false;
UltraEdit.activeDocument.findReplace.regExp=false;
UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
UltraEdit.activeDocument.findReplace.selectText=false;

// Run the replaces line by line till end of file is reached.
while (!UltraEdit.activeDocument.isEof()) {
   // Get current line number.
   linenr = UltraEdit.activeDocument.currentLineNum;
   // Get the word at start of every line.
   UltraEdit.activeDocument.selectWord();
   selword = UltraEdit.activeDocument.selection;
   // Move cursor to top of the file.
   UltraEdit.activeDocument.top();
   /* Run the replace all with the selected word as
      search and the line number as replace string. */
   UltraEdit.activeDocument.findReplace.replace(selword, String(linenr));
   // Move the cursor one line below last line to column 1.
   UltraEdit.activeDocument.gotoLine(linenr+1,1);
}

// At the end move cursor back to top of the file.
UltraEdit.activeDocument.top();
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Replacement with loop...

Postby rick_l » Fri Oct 12, 2007 8:18 pm

Thanks a lot Mofi! I am running the code now and hopefully it would get through soon. I really appreciate your help.
User avatar
rick_l
Newbie
 
Posts: 2
Joined: Wed Oct 10, 2007 11:00 pm


Return to Scripts