How to insert an increasing number at each Find?

How to insert an increasing number at each Find?

5
NewbieNewbie
5

    Feb 04, 2006#1

    Hi,

    I want a macro with this function:

    Find abc and

    for first find, add 1 before abc to get 1 abc,
    for second find, add 2 before abc to get 2 abc,
    ...
    for n find, add n before abc to get n abc.

    Please give a hand.

    6,601547
    Grand MasterGrand Master
    6,601547

      Feb 04, 2006#2

      Is it possible to use the Insert Number feature in column mode?

      That would not be so hard to use. It could be done without macro.


      If this is not possible here is a macro which could to the job. The UltraEdit macro language does not support variables and mathematical operations. Although I have written and posted macros which increases a number from 1 til 99 with a sequence of IfCharIs commands, I think that in this situation a different and easier method should be used.

      The macro first creates a new file and creates in a loop the specified number of empty lines. Modify the red highlighted loop number to specify the amount of numbers you need for your special replace in the source file. Run a find with your search string with Count All before macro execution and you will know how many numbers you will need.

      Next it uses the Insert Number feature in column mode to create the numbers. You could modify the green highlighted start number. It's also possible to create the numbers in hex or with leading zeros or with a different step. See options of ColumnInsertNum command.

      The following replace is used to collect all numbers in a single line. This is not really needed, if instead of Key DEL the command DeleteLine is used in the macro. But I think it is faster with this method. If you have enabled by default wrapping or only a low maximum number of characters per line then better use the method without the Replace but with DeleteLine.

      All preparations are done. The macro now starts the replace loop. In the loop it selects the current number and cuts it to user clipboard 9 and deletes the space (or line if DeleteLine is used here).

      Back to main source window and run the replace once. Modify this replace according to your needs. If the string is not found anymore, the temporary file with the numbers is deleted before the loop exits.

      If the amount of numbers was not enough, you will get an error message in the temporary file before the loop exits. The cursor in the main source file is then at end of the last replace. Look at the last number, modify the green start number in the macro, delete the temporary file with the error message and run the macro again to continue the replace job from current cursor position in the source file.

      Note: The macro always executes the replaces from current position in the main source file at start of the macro!

      Finally it clears user clipboard 9 and switches back to the windows clipboard.

      Note: This macro needs the macro property Continue if a Find with Replace not found enabled!

      InsertMode
      ColumnModeOn
      HexOff
      Clipboard 9
      NewFile
      Loop 100
      "
      "
      EndLoop
      Top
      ColumnInsertNum 1 1
      ColumnModeOff
      TrimTrailingSpaces
      Find "^p"
      Replace All " "

      Top
      Loop
      SelectWord
      Cut
      Key DEL
      NextWindow
      Find "abc"
      Replace "^c abc"

      IfNotFound
      PreviousWindow
      CloseFile NoSave
      ExitLoop
      Else
      PreviousWindow
      IfEof
      "Sorry, amount of numbers was not enough!"
      ExitLoop
      EndIf
      EndIf
      EndLoop
      ClearClipboard
      Clipboard 0
      Best regards from an UC/UE/UES for Windows user from Austria

      5
      NewbieNewbie
      5

        Feb 05, 2006#3

        Got it! Admire!

        14
        Basic UserBasic User
        14

          How to start numbers to insert not at 1 but at 0?

          Oct 17, 2017#4

          :D Also, to get this macro to work, which I finally did, had to change Replace "^c abc" with Paste. This of course changes each abc from original cursor to next number increment until end of file.

          And although I realized this is not the correct place to post this other unrelated question, Mofi please inform me if there's a way to associate a key stroke combo (hotkey) "goto previous position" (usually little green arrow top left menu cartouche...at least in my configuration ...)

          Thanks in advance Mofi :D

          6,601547
          Grand MasterGrand Master
          6,601547

            Oct 18, 2017#5

            luminarius, I do not understand why you wrote that Replace "^c abc" needed to be replaced by Paste. ^c in a non regular expression or an UltraEdit regular expression find/replace is replaced by current content of active clipboard whereby in an UltraEdit regular expression search/replace string the string in clipboard is also interpreted as regular expression string, i.e. the replacement of ^c by clipboard content is done before search/replace string is evaluated. In Unix or Perl regular expressions ^c as reference for clipboard content is not working because ^c means find character c at beginning of a line.

            The posted macro was written more than 11 years ago for a version of UltraEdit not supporting scripts. Such tasks can be much easier achieved in the meantime with an UltraEdit script because variables, mathematical operations and string manipulating operations can be used in UltraEdit scripts which is not possible in UltraEdit macros.

            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 replaces.
               UltraEdit.ueReOn();
               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;
            
               // Run a simple, case-insensitive, not regular expression replace
               // in a loop from top of file to end of file to insert a number
               // starting with value 0 before every abc in the file.
               var nNumber = 0;
               while(UltraEdit.activeDocument.findReplace.replace("abc",nNumber.toString(10)+" abc")) nNumber++;
            }
            
            Hotkeys or chords (multi-key assignment) can be assigned to internal commands of UltraEdit at Advanced - Settings or Configuration - Key mapping. The name of the command with tooltip Go to last position (UE < 23.00) or Go to previous position (UE ≥ 23.00) in the list of commands depends on version of UltraEdit for Windows. It is SearchGoBack with UE < v23.00 and Back (last position) with UE ≥ 23.00.
            Best regards from an UC/UE/UES for Windows user from Austria

            14
            Basic UserBasic User
            14

              Oct 18, 2017#6

              Yet for third world users with no scripting available and thus limited to macros ... and in my case just because I have a working macro version of this which is quite useful in automating C# format statements (transforms the output list directly into format string ...) the question remains (assuming no scripts available for whatever reason):

              Is there a way to get this macro to not start at '1' but to start at '0'? :D

              Furthermore, I hereby give gracious thanks for the script you published here on behalf of ue script users everywhere.... :D :D

              PS: I'm using UE 14 French. Could find neither Go to last position nor SearchGoBack in Advanced Settings keyboard map. :oops:

              6,601547
              Grand MasterGrand Master
              6,601547

                Oct 19, 2017#7

                The macro contains the command ColumnInsertNum 1 1 with green formatted number being the start number. Modify this number from 1 to 0 and numbering starts with 0.

                See also forum topics Incremental numbers at every found string and How to insert an incrementing number in a file using a counter in a macro?

                Regarding to command Back (last position) in menu Search of English UltraEdit for Windows v14.00b+1 look on the attached image.
                searchgoback.png (20.39KiB)
                Command SearchGoBack in Key Mapping configuration dialog of English UE v14.00b+1.
                Best regards from an UC/UE/UES for Windows user from Austria

                14
                Basic UserBasic User
                14

                  Mofi the Magnanimous does it again!

                  Oct 24, 2017#8

                  :D Thanks Mr. super competent and super knowledgeable Mofi (Google wide known). With your precise instructions I was able to
                  a) automate my C# format string generation starting with output field '0'
                  b) assign Alt+BACKSPACE to SearchGoBack in Configuration/Key Mapping.

                  :?: For other newbies out there, can you tell us what the numerical limits of the ColumnInsertNum 1 1 would be?

                  And, what happens if we change %APPDATA%\IDMComp\UltraEdit\Uedit32.shortcuts.txt directly by editing the text file. Will it automatically load when on UE instance creation in Windows 7? :?:

                  Thanks again. Felicitations de Paris :D :D

                  6,601547
                  Grand MasterGrand Master
                  6,601547

                    Oct 24, 2017#9

                    ColumnInsertNum is the macro command for Insert Number in menu Column. The first number is the starting number and the second number is the increment. Both numbers are 32-bit signed integers, i.e. range is -2147483648 to 2147483647 whereby larger numbers for increment usually do not make much sense. Inserting decrementing numbers is possible with a negative integer for increment number.

                    Editing the file %APPDATA%\IDMComp\UltraEdit\Uedit32.shortcuts.txt has no effect on hotkey assignments. This text file is just for documentation and includes hotkeys assigned to scripts in script list and currently loaded macros. The key assignments as customizable in configuration are stored binary depending on used version of UltraEdit for Windows in:
                    • uedit32.kbd
                    • uedit32.uek
                    • uedit64.uek
                    • uedit64u.uek
                    • uedit32u.uek
                    Best regards from an UC/UE/UES for Windows user from Austria