Search and Replace with substitution and increment

Help with writing and running scripts

Re: Search and Replace with substitution and increment

Postby spuzh » Fri Mar 02, 2012 5:30 am

How can I replace:
something1 x something2 something3
something4 x something5 something6
something7 x something8 something9

with:
something1 1100 something2 something3
something4 1101 something5 something6
something7 1102 something8 something9
spuzh
Newbie
 
Posts: 6
Joined: Sun Feb 19, 2012 12:12 pm

Re: Search and Replace with substitution and increment

Postby Mofi » Fri Mar 02, 2012 8:53 am

Why do you need a script for that task?

For the text you posted there is no need to replace x by an incrementing number with a script. This can be done easily with column mode features.

  1. Press Alt+C or click on Column - Column Mode to enable the column editing mode.
  2. Select the column with x using mouse or arrow keys.
    Special hint: Holding left Alt key and selecting with mouse makes a column selection without explicitly enabling column editing mode.
  3. Click on Column - Insert Number, enter in the dialog 1100 and click on button OK
  4. Press again Alt+C or click again on Column - Column Mode to disable the column editing mode. This last step is only needed when step 1 was not omitted because of making the selection with mouse while holding Alt key.
And that's it. There is no need for a script to insert an incrementing number within a column on all lines of a file or just some lines.

However, if you need a simple script for this simple task, here it is.

Code: Select all
if (UltraEdit.document.length > 0) {

   // Ask user of script for simple string to find.
   var sFind = UltraEdit.getString("Enter string to replace:",1);

   if (sFind != "") {  // Continue only if a string was entered.
      // Define environment for script.
      UltraEdit.insertMode();
      UltraEdit.columnModeOff();
      UltraEdit.activeDocument.hexOff();
      UltraEdit.ueReOn();

      // Define the fixed replace parameters for the script.
      UltraEdit.activeDocument.findReplace.mode=0;
      UltraEdit.activeDocument.findReplace.matchCase=false;
      UltraEdit.activeDocument.findReplace.matchWord=false;
      UltraEdit.activeDocument.findReplace.regExp=false;
      UltraEdit.activeDocument.findReplace.searchDown=true;
      if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean") {
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
      }
      UltraEdit.activeDocument.findReplace.preserveCase=false;
      UltraEdit.activeDocument.findReplace.replaceAll=false;
      UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;

      // Ask the user for the starting value.
      var nNumber = UltraEdit.getValue("Enter starting number:",1);

      // The replace is done from current position in file to end of file.
      while (UltraEdit.activeDocument.findReplace.replace(sFind,nNumber.toString())) {
         nNumber++;
      }
   }
}

Please note that you can't define the replace parameters like Match Case, Match Whole Word Only, Regular Expressions, etc. during script execution with the script as is. You need to do it in the script file itself. Or you add lots of extra lines asking the user of the script for all replace options and set the replace parameters accordingly.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3937
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Search and Replace with substitution and increment

Postby spuzh » Fri Mar 02, 2012 9:47 am

I've forgotten to mention that there is possibility to have some other lines between those ones that I've been stated.

Like:
something1 x something2 something3
something4 something5 something6
something7
x something8 something9

Thanks!
spuzh
Newbie
 
Posts: 6
Joined: Sun Feb 19, 2012 12:12 pm

Previous

Return to Scripts