Script for renumbering sequence identifiers with at least a four digit identifier

Script for renumbering sequence identifiers with at least a four digit identifier

7
NewbieNewbie
7

    17:19 - 15 days ago#1

    Can somebody create a script for UltraEdit/UEStudio with sequence with four digit identifier?

    Maximum ID length limit is 4.

    If id is 1 then sequence should be 0001.
    And if id is 11 then sequence should be 0011.
    If id is 1000 then sequence should be 1000.

    The sample tag: <section xml:id="c01-sec-000@">

    Code: Select all

    <section xml:id="c01-sec-0001">
    <section xml:id="c01-sec-0002">
    <section xml:id="c01-sec-0003">
    <section xml:id="c01-sec-0004">
    <section xml:id="c01-sec-0005">
    <section xml:id="c01-sec-0006">
    <section xml:id="c01-sec-0007">
    <section xml:id="c01-sec-0008">
    <section xml:id="c01-sec-0009">
    
    <section xml:id="c01-sec-0010">
    <section xml:id="c01-sec-0011">
    Actual id sequence like this:

    1 to 9:

    Code: Select all

    [0001]
    [0002]
    [0003]
    [0004]
    [0005]
    [0006]
    [0007]
    [0008]
    [0009]
    10 to 1000:

    Code: Select all

    [0010]
    [0011]
    [0012]
    [0013]
    [0014]
    [0015]
    [9999]
    [1000]

    6,633551
    Grand MasterGrand Master
    6,633551

      5:32 - 15 days ago#2

      Here is a simple script for this task.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
         // Move caret to top of the active file.
         UltraEdit.activeDocument.top();
      
         // Define the parameters for the Perl regular expression replace which
         // is run from top to end of the file in a loop until the searched
         // identifier cannot be found and replaced anymore in the active file.
         UltraEdit.perlReOn();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         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;
      
         // Define the variables required for the replace in the loop.
         var nNumber = 1;
         var sZeros = "0000";
         var sNumber = "0001";
         // \K in the Perl regular expression means keep back (unselect, unmatch)
         // the string already matched up to \K and select/match just the rest.
         // The backslash must be escaped with one more backslash in the JavaScript
         // String object for being interpreted by JavaScript as backslash character.
         while (UltraEdit.activeDocument.findReplace.replace('<section xml:id="c01-sec-\\K[^"]+',sNumber))
         {
            nNumber += 1;
            sNumber = nNumber.toString(10);
            if (nNumber < 1000)
            {  // Insert the correct amout of leading zeros on number less than 1000.
               sNumber = sZeros.substr(sNumber.length) + sNumber;
            }
         }
         UltraEdit.activeDocument.top();
      }
      
      There is nothing written about the file size. The script runs therefore Perl regular expression replaces directly on the file for supporting files even with several GB. That causes lots of window refreshes during script execution which takes much more time than the string replaces. The total execution time can be decreased by opening temporarily a new file displayed with maximized window and run the replaces on initially active file not visible now and then delete the temporarily opened new file without saving it. There are also lots of undo recordings made by UltraEdit during the replaces directly on the file.

      There would be a faster method for files with less than approximately 10 MB. The entire active file is selected and the entire file content is loaded as String object into the memory of the JavaScript engine. The string replaces are done on this String object in memory of the JavaScript engine. Then the entire selected text in active file is replaced by the String object with the renumbered identifiers. That would reduce the number of window refreshes to just three: select all at the beginning of the script execution and write over the selected text and moving caret to top of the active file at the end of the script execution. The number of undo records would be in this case just one because of the entire file is updated just once. If the active file is a Unicode encoded file (UTF-16, UTF-8) containing really non-ASCII characters, this approach requires also a full Unicode aware version of UltraEdit/UEStudio, i.e. UltraEdit for Windows ≥ 24.00 or UEStudio ≥ 17.00.

      Let us know if you would be interested in an optimized version for large and huge files with opening temporarily a new file or the solution doing the replaces in memory of the JavaScript engine for medium and small files for a minimum script execution time and just one undo record.
      Best regards from an UC/UE/UES for Windows user from Austria

      7
      NewbieNewbie
      7

        6:22 - 15 days ago#3

        This script is not running! can you please send with sample txt , where is run this script... 
        How to run this script?

        6,633551
        Grand MasterGrand Master
        6,633551

          8:47 - 14 days ago#4

          A sample file should be provided by the person asking for a script and not by the person who develops and of course tests the script. I attached the file which I used for testing the posted script.

          The following instructions are for UltraEdit for Windows and UEStudio v2024.0 for all three user interface modes as you have not written which version of UE/UES in which user interface mode is used by you.

          The script code must be copied into a new ASCII/ANSI encoded file saved with a file name like ReNumberIDs.js into a directory of your choice. I recommend creating a directory Scripts in the directory %APPDATA%\IDMComp\UltraEdit and save the script file with file extension JS in this directory. The script file must be next added to the Script list in UltraEdit. That can be done with:
          • Clicking in ribbon mode on last tab Advanced in the fourth group Script on the item All scripts.
          • Clicking in toolbar/menu mode with contemporary menus in menu Advanced on the menu item All scripts.
          • Clicking in toolbar/menu mode with traditional menus in menu Scripting on the menu item Scripts…
          There must be clicked the button Add, browsed to the script file and selected it, entered a meaningful description and clicked on button Open. A hotkey or chord (multi-key assignment) can be defined for the script for executing it on an opened file by pressing the defined hotkey or the defined key sequence.

          The execution of the script is done by opening the file with the sequence identifiers to renumber and pressing the hotkey/chord or clicking now in the list of scripts on the appropriate script file name. The script execution can be done either directly from the ribbon/menu or from the Script list window.

          The direct execution from ribbon/menu is done with:
          • Clicking in ribbon mode on last tab Advanced in the fourth group Script on the text of the item Play script (not on the graphical symbol) and click in the opened list of scripts on the appropriate script.
          • Clicking in toolbar/menu mode with contemporary menus in menu Advanced in the submenu Play script on the appropriate menu item.
          • Clicking in toolbar/menu mode with traditional menus in menu Scripting on the appropriate menu item.
          The Script list window can be opened with:
          • Clicking in ribbon mode on eight tab Layout in the third group Lists on the check box item Scripts.
          • Clicking in toolbar/menu mode with contemporary menus in eight  menu Layout on the menu item Scripts.
          • Clicking in toolbar/menu mode with traditional menus in sixth menu View in submenu Views/lists in submenu Lists on the menu item Script list.
          A script in the displayed window Script list can be executed on the active opened file by double clicking on the script file name in the list.
          script_test_file.zip (4.68 KiB)   0
          ZIP file with a text file created for testing the script.
          Best regards from an UC/UE/UES for Windows user from Austria