How to insert an incrementing number in a file using a counter in a macro?

How to insert an incrementing number in a file using a counter in a macro?

2
NewbieNewbie
2

    Feb 24, 2006#1

    Hi,

    Sorry if my question sounds dumb because it should be very easy to do but I can't find any explanation anywhere.

    I have some files who are formatted this way:

    Code: Select all

    000:000
    data go here
    
    000:000
    data go here
    
    000:000
    data go here
    ...
    
    All I want to do is insert a counter that goes from 1 to the number of data blocks in the file on a new line before each block.

    Something like that:

    Code: Select all

    $counter=1
    Loop
    Find RegExp "^([0-9]+:[0-9]+^)"
    IfFound
    Replace "$counter^p^1"
    $counter++;
    Else
    ExitLoop
    EndIf
    EndLoop
    
    Except that I can't find how to set up a basic variable and increment it. ($counter in my example of course!)

    Any idea please?

    PS: At the end I would like to have something like that :

    Code: Select all

    1
    000:000
    data go here
    
    2
    000:000
    data go here
    
    3
    000:000
    data go here
    
    ...
    
    3565
    000:000
    data go here
    

    344
    MasterMaster
    344

      Feb 24, 2006#2

      Howdy,

      unfortunately UltraEdit macros do not support variables.

      But Mofi found a workaround for that, look here: How to insert an increasing number at each Find?

      rds Bego
      Normally using all newest english version incl. each hotfix. Win 10 64 bit

      2
      NewbieNewbie
      2

        Feb 27, 2006#3

        Gosh ! I thought such basic feature would be integrated. :/

        Thank you for the tips but this won't work for me since the counter is limited to 99 and in my case I need thousand of values.

        344
        MasterMaster
        344

          Feb 27, 2006#4

          You're running in open doors here, dude.

          I think a better macro support is the no.1 on my personal improvement hit list. Variables, nested loops and indents would enhance the macro possibilities a lot. :?
          Anyway, many things can be done with the status quo. :idea:

          rds Bego
          Normally using all newest english version incl. each hotfix. Win 10 64 bit

          6,602547
          Grand MasterGrand Master
          6,602547

            Feb 27, 2006#5

            Nevertheless you need thousands, this could be done with my macro. Just adjust the red loop number as explained for example to 500000 and you will be able to count up to 499999.

            There is also a second method to count up a number by 1 explained at Is this possible? Find X And Replace With X+1. With the following improved version it has no range limit. But you need 2 macros for this solution because 2 loops are needed and currently nested loops are not supported. The red number in the main macro is the initial value of the counter which is increased by 1 before first use.

            Your main macro:

            InsertMode
            ColumnModeOff
            HexOff
            UnixReOff
            Top
            Clipboard 7
            "0"
            StartSelect
            Key LEFT ARROW
            Cut
            EndSelect
            Loop
            Find RegExp "[0-9]+:[0-9]+"
            IfNotFound
            ExitLoop
            EndIf
            Key HOME
            IfColNumGt 1
            Key HOME
            EndIf
            "
            "
            Key UP ARROW
            PlayMacro 1 "CountUp"
            Key DOWN ARROW
            Key END
            EndLoop
            ClearClipboard
            Clipboard 0


            The universal macro CountUp has following code:

            Paste
            EndSelect
            InsertMode
            "|"
            Key LEFT ARROW
            Key LEFT ARROW
            OverStrikeMode
            Loop
            IfCharIs "0"
            "1"
            ExitLoop
            EndIf
            IfCharIs "1"
            "2"
            ExitLoop
            EndIf
            IfCharIs "2"
            "3"
            ExitLoop
            EndIf
            IfCharIs "3"
            "4"
            ExitLoop
            EndIf
            IfCharIs "4"
            "5"
            ExitLoop
            EndIf
            IfCharIs "5"
            "6"
            ExitLoop
            EndIf
            IfCharIs "6"
            "7"
            ExitLoop
            EndIf
            IfCharIs "7"
            "8"
            ExitLoop
            EndIf
            IfCharIs "8"
            "9"
            ExitLoop
            EndIf
            IfCharIs "9"
            "0"
            Key LEFT ARROW
            IfColNum 1
            InsertMode
            "1"
            ExitLoop
            EndIf
            Key LEFT ARROW
            IfCharIs "0123456789"
            Else
            Key RIGHT ARROW
            InsertMode
            "1"
            ExitLoop
            EndIf
            EndIf
            EndLoop
            InsertMode
            Loop
            IfColNum 1
            ExitLoop
            EndIf
            Key LEFT ARROW
            IfCharIs "0123456789"
            Else
            Key RIGHT ARROW
            ExitLoop
            EndIf
            EndLoop
            StartSelect
            Find Select "|"
            Key LEFT ARROW
            Copy
            EndSelect
            Key RIGHT ARROW
            Key LEFT ARROW
            Key DEL



            This macro is really universal. It uses the clipboard of the main macro and assumes that the initial or last number is stored in the clipboard. So the number can be manipulated by the main macro at every time. It is able to count up the number even inside a term like var[005] and does already recognize existing parts of a number if the cursor and the edit mode is set correct before calling macro CountUp. Leading zeros is supported if the initial string number has the right amount of leading zeros.
            Best regards from an UC/UE/UES for Windows user from Austria

            1
            NewbieNewbie
            1

              Sep 29, 2006#6

              Hey Mofi, hope you are browsing this as I am going out of my mind. I tried using this counter but I keep getting a "Search string not found" error when I run the following code. Basically it is taking a big file and splitting it up into smaller ones. Any help would be appreciated.

              FIRST MACRO:

              Code: Select all

              InsertMode
              ColumnModeOff
              HexOff
              Top
              "000
              "
              Top
              StartSelect
              Key END
              Clipboard 8
              Cut 
              EndSelect
              Key BACKSPACE
              Clipboard 9
              Loop 
              IfEof
              ExitLoop
              EndIf
              GotoLine 15000
              Key END
              SelectToTop
              Cut 
              EndSelect
              DeleteLine
              NewFile
              "C:\tempDir\tempFile-000.csv
              "
              Paste 
              Top
              Find "000"
              Clipboard 8
              PlayMacro 1 "CountUp"
              Clipboard 9
              Key HOME
              StartSelect
              Key END
              Cut 
              EndSelect
              Key BACKSPACE
              SaveAs "^c"
              CloseFile
              EndLoop
              Clipboard 9
              ClearClipboard
              Clipboard 8
              ClearClipboard
              Clipboard 0
              YOUR MACRO: See CountUp above.

              344
              MasterMaster
              344

                Sep 29, 2006#7

                I'm not Mofi ;-) but: Did you set the macro property "Continue if a Find with Replace not found"?

                Bego
                Normally using all newest english version incl. each hotfix. Win 10 64 bit

                6,602547
                Grand MasterGrand Master
                6,602547

                  Sep 29, 2006#8

                  Hi Cleitus and Bego!

                  Checking Continue if a Find with Replace not found is normally the correct advice, but not in this case. The 2 finds in the 2 macros should always find the string because the search string was inserted before by the macro itself.

                  By the way: a better english name for this property would be "Continue if a Find not found".

                  But here I think the problem is what I have posted in my first post at Perl RegExs misbehaving in v12.10b. In v12.10b of UltraEdit and v6.00a of UEStudio a normal non-regex find executed from within a macro never finds the string when the Perl compatible regular expression engine is active. In your first macro there is no regex engine selecting macro command because you don't use any regex search. But if you have the Perl engine selected in the configuration dialog, the non regex finds in the macro are not working nevertheless because the Perl engine is active.

                  Solution: Switch temporarily to an other regex engine.

                  I'm also wondering about the Key BACKSPACE in your macro. I think you want to delete the line inserted at top of the file, but Key BACKSPACE executed at top of the file will not do it. Key DEL or macro command DeleteLine would be better.

                  Here is my solution for your macro. The macro property Continue if a Find with Replace not found should have no influence on macro execution.

                  InsertMode
                  ColumnModeOff
                  HexOff
                  UnixReOff
                  Top
                  "000"
                  StartSelect
                  Key HOME
                  Clipboard 8
                  Cut
                  EndSelect
                  Clipboard 9
                  Loop
                  IfEof
                  ExitLoop
                  EndIf
                  StartSelect
                  GotoLineSelect 15000
                  Key END
                  Cut
                  EndSelect
                  DeleteLine
                  NewFile
                  "C:\tempDir\tempFile-000.csv
                  "
                  Paste
                  "
                  "

                  Top
                  Find "000"
                  Clipboard 8
                  PlayMacro 1 "CountUp"
                  Clipboard 9
                  Key HOME
                  StartSelect
                  Key END
                  Cut
                  EndSelect
                  DeleteLine
                  SaveAs "^c"
                  CloseFile
                  EndLoop
                  Clipboard 9
                  ClearClipboard
                  Clipboard 8
                  ClearClipboard
                  Clipboard 0
                  PerlReOn

                  You can remove the green " lines if you don't want it. But I personally don't like text files where the last line of the file is not terminated with CRLF or with LF only or with CR only depending on the file type DOS/UNIX/MAC.
                  Best regards from an UC/UE/UES for Windows user from Austria

                  49
                  Basic UserBasic User
                  49

                    Mar 19, 2016#9

                    I have some text as

                    Code: Select all

                    id="##"
                    abcd
                    id="##"
                    lkdfgg
                    id="##"
                    nkvxv1232
                    id="##"
                    xvnm,xcvnvn
                    id="##"
                    id="##"
                    id="##"
                    id="##"
                    id="##"
                    id="##"
                    id="##"
                    
                    s/b

                    Code: Select all

                    id="1"
                    abcd
                    id="2"
                    lkdfgg
                    id="3"
                    nkvxv1232
                    id="4"
                    xvnm,xcvnvn
                    id="5"
                    id="6"
                    id="7"
                    id="8"
                    id="9"
                    id="10"
                    id="11"
                    
                    How to use your macro for above treatment?
                    How to modify this macro?
                    Please help me.

                    6,602547
                    Grand MasterGrand Master
                    6,602547

                      Mar 19, 2016#10

                      1. Select and copy source code of macro CountUp to clipboard.
                      2. In UltraEdit/UEStudio click on Macro - Edit Macro.
                      3. Click on button New Macro.
                      4. Enter CountUp as macro name.
                      5. Uncheck first option and let second option checked.
                      6. Click on button OK.
                      7. Select existing code of new macro with mouse and press Ctrl+V to paste copied macro code replacing the selection.
                      8. Click on button Close and confirm the question with a click on button Yes.
                      Redo the steps above with the code below and any name you want for the second new macro.

                      Code: Select all

                      InsertMode
                      ColumnModeOff
                      HexOff
                      UnixReOff
                      Top
                      Clipboard 7
                      "0"
                      StartSelect
                      Key LEFT ARROW
                      Cut
                      EndSelect
                      Loop
                      Find MatchCase "id="#"
                      IfNotFound
                      ExitLoop
                      EndIf
                      Key LEFT ARROW
                      Find MatchCase RegExp "#+"
                      PlayMacro 1 "CountUp"
                      EndLoop
                      ClearClipboard
                      Clipboard 0
                      Top
                      
                      Run this macro on active file using Macro - Play Again.
                      Best regards from an UC/UE/UES for Windows user from Austria