How to insert incremental numbers after specific text

How to insert incremental numbers after specific text

9
NewbieNewbie
9

    Jan 02, 2011#1

    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

    6,687587
    Grand MasterGrand Master
    6,687587

      Jan 02, 2011#2

      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();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.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.

      9
      NewbieNewbie
      9

        Jan 03, 2011#3

        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.

        6,687587
        Grand MasterGrand Master
        6,687587

          Jan 03, 2011#4

          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();
             if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
             else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.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++));
          }

          9
          NewbieNewbie
          9

            Jan 03, 2011#5

            Wow excellent

            You are genius..

            That is working perfectly

            Thanks a lot

            thank you very much

              Jan 10, 2011#6

              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

              6,687587
              Grand MasterGrand Master
              6,687587

                Jan 10, 2011#7

                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();
                   if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                   else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.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;
                   }
                }

                9
                NewbieNewbie
                9

                  Jan 10, 2011#8

                  fine fine

                  working well

                  thanks a lot

                    Jan 12, 2011#9

                    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

                    6,687587
                    Grand MasterGrand Master
                    6,687587

                      Jan 12, 2011#10

                      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/j ... 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++)
                      Best regards from an UC/UE/UES for Windows user from Austria

                      10
                      Basic UserBasic User
                      10

                        Mar 09, 2012#11

                        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!

                        6,687587
                        Grand MasterGrand Master
                        6,687587

                          Mar 09, 2012#12

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

                          Code: Select all

                          if (UltraEdit.document.length > 0) {
                             UltraEdit.insertMode();
                             if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                             else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.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();
                             if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                             else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.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 1: 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++;
                          }
                          Update 2: With UE v19.00 and UES v13.00 the text replaced by a Perl regular expression replace is no longer selected and therefore the script works without a workaround code for discarding the selection.

                          10
                          Basic UserBasic User
                          10

                            Mar 09, 2012#13

                            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!

                            6,687587
                            Grand MasterGrand Master
                            6,687587

                              Mar 09, 2012#14

                              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();
                                 if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                                 else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.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();
                                 }
                              }

                              10
                              Basic UserBasic User
                              10

                                Mar 09, 2012#15

                                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!