How to insert incremental numbers after specific text

Help with writing and running scripts

How to insert incremental numbers after specific text

Postby mprlak » Sun Jan 02, 2011 11:42 am

Hi!

I want insert the numbers incrementally to specific word like

if found
sec
sec
sec
sec
replace with
sec1
sec2
sec3
sec4

Looking for script for above mentioned.

Thanks in advance
mprlak
Newbie
 
Posts: 9
Joined: Sat Jan 01, 2011 2:23 am

Re: How to insert incremental numbers after specific text

Postby Mofi » Sun Jan 02, 2011 1:28 pm

If the words are all in the same column on consecutive lines, switch to column mode with Column - Column Mode, select the column on the lines where you want to insert / replace the numbers and use Column - Insert Number. Finally turn column editing mode off by clicking again on Column - Column Mode.

For example a script posted at Increment from value found in file adapted to your needs:

Code: Select all
if (UltraEdit.document.length > 0) {
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();
   UltraEdit.activeDocument.hexOff();
   UltraEdit.perlReOn();
   var nNumber = 1;
   UltraEdit.activeDocument.top();
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.searchDown=true;
   UltraEdit.activeDocument.findReplace.matchCase=true;
   UltraEdit.activeDocument.findReplace.matchWord=true;
   UltraEdit.activeDocument.findReplace.regExp=false;
   UltraEdit.activeDocument.findReplace.preserveCase=false;
   UltraEdit.activeDocument.findReplace.replaceAll=false;
   UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
   while (UltraEdit.activeDocument.findReplace.replace("sec","sec"+nNumber++));
}

For a more enhanced version see Need Search and replace with substitution and increment.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: How to insert incremental numbers after specific text

Postby mprlak » Mon Jan 03, 2011 1:18 am

Thank you, that is working well.

And I want not only "sec", any word which I selected in the document.

Kindly send for the same as above.
mprlak
Newbie
 
Posts: 9
Joined: Sat Jan 01, 2011 2:23 am

Re: How to insert incremental numbers after specific text

Postby Mofi » Mon Jan 03, 2011 4:41 am

Doing that for the currently selected word can be easily realized too.

Code: Select all
if (UltraEdit.document.length > 0 && UltraEdit.activeDocument.isSel()) {
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();
   UltraEdit.activeDocument.hexOff();
   UltraEdit.perlReOn();
   var nNumber = 1;
   var sWord = UltraEdit.activeDocument.selection;
   UltraEdit.activeDocument.top();
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.searchDown=true;
   UltraEdit.activeDocument.findReplace.matchCase=true;
   UltraEdit.activeDocument.findReplace.matchWord=true;
   UltraEdit.activeDocument.findReplace.regExp=false;
   UltraEdit.activeDocument.findReplace.preserveCase=false;
   UltraEdit.activeDocument.findReplace.replaceAll=false;
   UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
   while (UltraEdit.activeDocument.findReplace.replace(sWord,sWord+nNumber++));
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: How to insert incremental numbers after specific text

Postby mprlak » Mon Jan 03, 2011 6:51 am

Wow excellent

You are genius..

That is working perfectly

Thanks a lot

thank you very much
mprlak
Newbie
 
Posts: 9
Joined: Sat Jan 01, 2011 2:23 am

Re: How to insert incremental numbers after specific text

Postby mprlak » Mon Jan 10, 2011 9:49 am

Hi

In the above coding working well, but I want msg box for starting number and ending number

Ex:
Chapter-1
Sec1
Sec2
Sec3
Sec4

Chapter-2
Sec1
Sec2
Sec3

look the above example, I want the code for like that

thanks in advance
mprlak
Newbie
 
Posts: 9
Joined: Sat Jan 01, 2011 2:23 am

Re: How to insert incremental numbers after specific text

Postby Mofi » Mon Jan 10, 2011 12:00 pm

I hope, this script does what you want. Please note that command top() is commented now. So this script runs the replace from current selection to end of file or up to the entered ending number.

Code: Select all
if (UltraEdit.document.length > 0 && UltraEdit.activeDocument.isSel()) {
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();
   UltraEdit.activeDocument.hexOff();
   UltraEdit.perlReOn();
   var sWord = UltraEdit.activeDocument.selection;
   var nNumber = UltraEdit.getValue("Enter starting number:",1);
   var nEndval = UltraEdit.getValue("Enter ending number:",1);
   // UltraEdit.activeDocument.top();
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.searchDown=true;
   UltraEdit.activeDocument.findReplace.matchCase=true;
   UltraEdit.activeDocument.findReplace.matchWord=true;
   UltraEdit.activeDocument.findReplace.regExp=false;
   UltraEdit.activeDocument.findReplace.preserveCase=false;
   UltraEdit.activeDocument.findReplace.replaceAll=false;
   UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
   while (nNumber <= nEndval) {
      if (!UltraEdit.activeDocument.findReplace.replace(sWord,sWord+nNumber++)) break;
   }
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: How to insert incremental numbers after specific text

Postby mprlak » Mon Jan 10, 2011 12:24 pm

fine fine

working well

thanks a lot
mprlak
Newbie
 
Posts: 9
Joined: Sat Jan 01, 2011 2:23 am

Re: How to insert incremental numbers after specific text

Postby mprlak » Wed Jan 12, 2011 12:47 am

Hi

One more script want about the above mentioned sequenced number.

this time i want roman letters to enter incrementally.

pls send me.

Thanx in advance
mprlak
Newbie
 
Posts: 9
Joined: Sat Jan 01, 2011 2:23 am

Re: How to insert incremental numbers after specific text

Postby Mofi » Wed Jan 12, 2011 8:13 am

I searched the web for a Javascript function converting decimal numbers to roman numbers and found the page

http://www.iandevlin.com/blog/2010/03/javascript/converting-decimal-numbers-to-roman-numerals-in-javascript

I think, the function for simple conversion is enough for your needs. Copy and paste following code (just variable names changed and return value is now an empty string on number out of range) to top of the script file:

Code: Select all
var g_sRomanLetters  = new Array("M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I");
var g_nDecimalValues = new Array(1000,900,500,400,100,90,50,40,10,9,5,4,1);

function decimalToRomanSimple(nValue) {
  var sRomanNumeral = "";
  if (nValue <= 0 || nValue >= 4000) return sRomanNumeral;
  for (var i = 0; i < g_sRomanLetters.length; i++) {
    while (nValue >= g_nDecimalValues[i]) {
      nValue -= g_nDecimalValues[i];
      sRomanNumeral += g_sRomanLetters[i];
    }
  }
  return sRomanNumeral;
}

And replace nNumber++ in the replace command by decimalToRomanSimple(nNumber++)
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: How to insert incremental numbers after specific text

Postby EricT » Thu Mar 08, 2012 8:30 pm

Hi all!

Mofi I have been reading through several of your scripts here, and have tried to adapt a few different ones for my own desired use but I haven't been able to get things working as I would like. This topic seems to be very closely related so I thought I would ask in here.

I'm trying to do almost exactly as the OP is, but instead of after text, replace numbers with numbers. For example

Replace:

1._
2._
4._
7._

(where underlines are spaces)

With

1._
2._
3._
4._

So basically, find "^[0-9]+\. " and replace with "d%\. " where d% starts as the number 1, and increments by one for each replacement. The period and space following are just static items.

The big difference between what I am asking for and whats already been posted is, the find is based on any number, and it renumbers starting with 1 every time - so even if the list started with 27, that would be replaced with a 1 and continued from there.

I hope that's all clear. Thanks!
EricT
Newbie
 
Posts: 5
Joined: Thu Mar 08, 2012 8:22 pm

Re: How to insert incremental numbers after specific text

Postby Mofi » Fri Mar 09, 2012 2:12 am

Your task can be done with following script using UltraEdit regular expression engine.

Code: Select all
if (UltraEdit.document.length > 0) {
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();
   UltraEdit.activeDocument.hexOff();
   UltraEdit.ueReOn();
   UltraEdit.activeDocument.top();
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.matchCase=false;
   UltraEdit.activeDocument.findReplace.matchWord=false;
   UltraEdit.activeDocument.findReplace.regExp=true;
   UltraEdit.activeDocument.findReplace.searchDown=true;
   UltraEdit.activeDocument.findReplace.searchInColumn=false;
   UltraEdit.activeDocument.findReplace.preserveCase=false;
   UltraEdit.activeDocument.findReplace.replaceAll=false;
   UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
   var nNumber = 0;
   var sReplace = "";
   do {
      nNumber++;
      sReplace = nNumber.toString() + ". ";
   } while (UltraEdit.activeDocument.findReplace.replace("%[0-9]+. ",sReplace));
}

The code below should do the same using Perl regular expression engine, but results in UE v18.00.0.1025 in incrementing in an endless loop the first occurrence of a number. That is definitely a bug in UE v18.00.0.1025 which I will report to IDM support by email.

Code: Select all
if (UltraEdit.document.length > 0) {
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();
   UltraEdit.activeDocument.hexOff();
   UltraEdit.perlReOn();
   UltraEdit.activeDocument.top();
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.matchCase=false;
   UltraEdit.activeDocument.findReplace.matchWord=false;
   UltraEdit.activeDocument.findReplace.regExp=true;
   UltraEdit.activeDocument.findReplace.searchDown=true;
   UltraEdit.activeDocument.findReplace.searchInColumn=false;
   UltraEdit.activeDocument.findReplace.preserveCase=false;
   UltraEdit.activeDocument.findReplace.replaceAll=false;
   UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
   var nNumber = 0;
   var sReplace = "";
   do {
      nNumber++;
      sReplace = nNumber.toString() + ". ";
   } while (UltraEdit.activeDocument.findReplace.replace("^[0-9]+\\. ",sReplace));
}

Update: The script above with the Perl regular expression results in an endless loop since v14.00 of UltraEdit. In UE v13.20 the script works as is. In UE v13.00 and v13.10 the numbers are found, but not replaced. The reason for the endless loop with UE v14.00 and later is the fact that a replaced string is still selected after replace when using the Perl regular expression engine while there is no selection anymore when doing a non regular expression, UE regular expression or Unix regular expression replace. This different behavior on selection after replace can be seen also with making the replace manually with Perl regular expression enabled.

Another possible solution for UE v17.20 and later using Perl is:

Code: Select all
do {
   nNumber++;
   sReplace = nNumber.toString() + ". ";
   UltraEdit.activeDocument.cancelSelect();
} while (UltraEdit.activeDocument.findReplace.replace("^[0-9]+\\. ",sReplace));

For UE v14.00 to v17.10 not supporting cancelSelect() following can be used with Perl:

Code: Select all
do {
   nNumber++;
   sReplace = nNumber.toString() + ". ";
   UltraEdit.activeDocument.findReplace.replace("^[0-9]+\\. ",sReplace);
   UltraEdit.activeDocument.key("RIGHT ARROW");
} while (UltraEdit.activeDocument.isFound());

And the last solution working for all versions of UltraEdit since v13.00, the first version of UE supporting scripts, using Perl:

Code: Select all
var nNumber = 1;
while (UltraEdit.activeDocument.findReplace.find("^[0-9]+\\. ")) {
   UltraEdit.activeDocument.write(nNumber + ". ");
   nNumber++;
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: How to insert incremental numbers after specific text

Postby EricT » Fri Mar 09, 2012 10:13 am

Thanks, Mofi, that does exactly what I need. Great catch on the perl loop there.

Is there anything I can do to change this so that it runs on all open files?

Thanks again!
EricT
Newbie
 
Posts: 5
Joined: Thu Mar 08, 2012 8:22 pm

Re: How to insert incremental numbers after specific text

Postby Mofi » Fri Mar 09, 2012 12:15 pm

Making the renumbering on all open files is quite easy as you can see below. I added an extra top() command to have the caret at top of every processed file after script finished. Files currently open in hex edit mode are ignored now.

Code: Select all
if (UltraEdit.document.length > 0) {
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();
   UltraEdit.ueReOn();
   for (nDocIndex = 0; nDocIndex < UltraEdit.document.length; nDocIndex++) {
      if (UltraEdit.document[nDocIndex].hexMode == true) continue;
      UltraEdit.document[nDocIndex].top();
      UltraEdit.document[nDocIndex].findReplace.mode=0;
      UltraEdit.document[nDocIndex].findReplace.matchCase=false;
      UltraEdit.document[nDocIndex].findReplace.matchWord=false;
      UltraEdit.document[nDocIndex].findReplace.regExp=true;
      UltraEdit.document[nDocIndex].findReplace.searchDown=true;
      UltraEdit.document[nDocIndex].findReplace.searchInColumn=false;
      UltraEdit.document[nDocIndex].findReplace.preserveCase=false;
      UltraEdit.document[nDocIndex].findReplace.replaceAll=false;
      UltraEdit.document[nDocIndex].findReplace.replaceInAllOpen=false;
      var nNumber = 0;
      var sReplace = "";
      do {
         nNumber++;
         sReplace = nNumber.toString() + ". ";
      } while (UltraEdit.document[nDocIndex].findReplace.replace("%[0-9]+. ",sReplace));
      UltraEdit.document[nDocIndex].top();
   }
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: How to insert incremental numbers after specific text

Postby EricT » Fri Mar 09, 2012 12:41 pm

I might be missing something here.

The first script posted works, and works well, on one file at a time.

Your last one that should run on all open files outputs this in the output window:

An error occurred on line 1:
 ???
Script failed.

And doesn't affect any changes on any open files.

Thanks!



Mofi wrote:I think, you copied the script code into a Unicode file which starts with a BOM (Byte Order Mark - hidden bytes at beginning of file). Javascript does not support Unicode scripts. Convert the file to ASCII with DOS line terminators. With a script file opened in UltraEdit you should see just DOS in third box in the status bar at bottom of the UltraEdit main window.

That is exactly correct, good call Mofi. Much appreciated!
EricT
Newbie
 
Posts: 5
Joined: Thu Mar 08, 2012 8:22 pm


Return to Scripts