Community
    • Login

    Help REPLACING content with incremental order (by +1)

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    10 Posts 5 Posters 1.1k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Joel RodriguesJ
      Joel Rodrigues
      last edited by Joel Rodrigues

      Hi folks,

      In my TXT file I have several lines with the following content:

      "HandleId": "1",
      "HandleId": "13",
      "HandleId": "5",
      "HandleId": "2",
      "HandleId": "11",
      

      I would like to replace the number in a ascendent incremental order by +1, so the lines above would change to:

      "HandleId": "1",
      "HandleId": "2",
      "HandleId": "3",
      "HandleId": "4",
      "HandleId": "5",
      

      It seems easy, but imagine that I have thousands of lines in this context.

      Could someone help me with this, please?

      best regards,

      JR

      PeterJonesP supasillyassS CoisesC 3 Replies Last reply Reply Quote 1
      • PeterJonesP
        PeterJones @Joel Rodrigues
        last edited by

        @Joel-Rodrigues said in Help REPLACING content with incremental order (by +1):

        Could someone help me with this, please?

        Our FAQ section has a whole writeup on mathematical replacement. Go read that, and use one of the methods suggested there.

        1 Reply Last reply Reply Quote 2
        • supasillyassS
          supasillyass @Joel Rodrigues
          last edited by

          @Joel-Rodrigues Something like this is better handled by a scripting language, but you can be a little cheeky with the Search Results Window and copy its output:

          Search "HandleId" (5 hits in 1 file of 1 searched)
            new 1 (5 hits)
          	Line 1: "HandleId": "1",
          	Line 2: "HandleId": "13",
          	Line 3: "HandleId": "5",
          	Line 4: "HandleId": "2",
          	Line 5: "HandleId": "11",
          

          Then use a RegEx search for “: "HandleId":.*” and replace with “",”:

          Search "HandleId" (5 hits in 1 file of 1 searched)
            new 1 (5 hits)
          	Line 1",
          	Line 2",
          	Line 3",
          	Line 4",
          	Line 5",
          

          And again RegEx search for “^\s+Line\s+” and replace with “"HandleId": "”:

          Search "HandleId" (5 hits in 1 file of 1 searched)
            new 1 (5 hits)
          "HandleId": "1",
          "HandleId": "2",
          "HandleId": "3",
          "HandleId": "4",
          "HandleId": "5",
          

          Then just delete the first two lines.

          1 Reply Last reply Reply Quote 3
          • CoisesC
            Coises @Joel Rodrigues
            last edited by Coises

            @Joel-Rodrigues said in Help REPLACING content with incremental order (by +1):

            Hi folks,

            In my TXT file I have several lines with the following content:

            "HandleId": "1",
            "HandleId": "13",
            "HandleId": "5",
            "HandleId": "2",
            "HandleId": "11",
            

            I would like to replace the number in a ascendent incremental order by +1, so the lines above would change to:

            "HandleId": "1",
            "HandleId": "2",
            "HandleId": "3",
            "HandleId": "4",
            "HandleId": "5",
            

            It seems easy, but imagine that I have thousands of lines in this context.

            The simplest solution depends a lot on whether you’ve described the problem precisely or omitted some details. If all the lines are the same except for the number, and they are all together (nothing else intervening), you can do it this way:

            First, make a rectangular selection that encloses all the numbers and the quotes and commas following them. To do that, click between the number and the preceding quote on the first line; then scroll down using the scroll bar (don’t click in the document or use your keyboard) to the end of the lines you want to change, then hold down Shift and Alt on your keyboard and click on the last line somewhere well to the right of the longest line. You’ll see a selection that covers all the numbers and the closing ", on each line, plus some extra empty space at the right. It doesn’t matter how much extra space you cover on the right, so long as you get past the end of every line in the selection.

            Now, type ", which will leave you with a series of lines ending in "",.

            Now, make another rectangular selection starting between the two quote marks at the end of the first line and ending between the two quote marks at the end of the last line. This will have no width (so it will just look like a single, thin vertical line) and will extend across all the lines you are changing.

            Next, choose Edit | Column Editor from the main menu. Enter:
            Number to insert (selected)
            Initial number: 1
            Increase by: 1
            Repeat: 1
            Leading: None
            Format: Dec
            and click OK.

            You should now have what you want.

            1 Reply Last reply Reply Quote 3
            • Joel RodriguesJ
              Joel Rodrigues
              last edited by

              Hi folks and thank you so much for your kind attention and support.

              Maybe as was highlighted, I should have provided more details.

              The lines with “HandleID” content are not in sequence. They are as follows:

              "HandleId": "1",
              (some text here)
              (some text here)
              "HandleId": "13",
              (some text here)
              (some text here)
              (some text here)
              (some text here)
              "HandleId": "5",
              "HandleId": "2",
              (some text here)
              (some text here)
              (some text here)
              (some text here)
              (some text here)
              (some text here)
              (some text here)
              "HandleId": "11",
              (some text here)
              

              My intention would to be presented as follows:

              "HandleId": "1",
              (some text here)
              (some text here)
              "HandleId": "2",
              (some text here)
              (some text here)
              (some text here)
              (some text here)
              "HandleId": "3",
              "HandleId": "4",
              (some text here)
              (some text here)
              (some text here)
              (some text here)
              (some text here)
              (some text here)
              (some text here)
              "HandleId": "5",
              (some text here)
              

              It´s not necessary to also move the “(some text here)” content together with the HandleID … I just need to change the HandleID number.

              Sorry for not providing this detail before.

              best regards

              JR

              CoisesC 1 Reply Last reply Reply Quote 0
              • CoisesC
                Coises @Joel Rodrigues
                last edited by

                @Joel-Rodrigues said in Help REPLACING content with incremental order (by +1):

                The lines with “HandleID” content are not in sequence. They are as follows:

                "HandleId": "1",
                (some text here)
                (some text here)
                "HandleId": "13",
                (some text here)
                (some text here)
                (some text here)
                (some text here)
                "HandleId": "5",
                "HandleId": "2",
                (some text here)
                (some text here)
                (some text here)
                (some text here)
                (some text here)
                (some text here)
                (some text here)
                "HandleId": "11",
                (some text here)
                

                My intention would to be presented as follows:

                "HandleId": "1",
                (some text here)
                (some text here)
                "HandleId": "2",
                (some text here)
                (some text here)
                (some text here)
                (some text here)
                "HandleId": "3",
                "HandleId": "4",
                (some text here)
                (some text here)
                (some text here)
                (some text here)
                (some text here)
                (some text here)
                (some text here)
                "HandleId": "5",
                (some text here)
                

                In that case, there are a few options.

                The easiest, in my opinion, would be to use the Columns++ plugin. (However, I wrote that plugin, so perhaps I am biased.):

                Columns++ isn’t yet in the Plugins Admin for Notepad++; so you’ll need to install it either using the Quick Installer link listed for the latest version, or, if you can’t or don’t want to use that, following the instructions here.

                After installing Columns++, open your file in Notepad++.

                Select Plugins | Columns++ | Search…, enter:

                Find what : "HandleId":\h*"\d+"
                Replace with : "HandleId": "(?=match)"
                Search Mode: Regular expression

                and click Replace All.

                The MultiReplace plugin, which can be installed from Plugins Admin, can do this in a way similar to Columns++, though the details are different.

                If you do not want to install a plugin, your problem can be solved with only Notepad++ built-in functions, using a method similar to what I described in my previous post:

                • First insert an increasing number at the beginning of every line in the file. For this, be sure to set Leading: Zeros in the Column Editor dialog so they are all the same width.

                • Then make a rectangular selection of the parts after the added line numbers and sort on that — that will put all the lines beginning with HandleId together.

                • Then replace the numbers on the (now contiguous) HandleId lines the same way I described in my previous post.

                • Then sort the file with nothing selected, which will put the lines back in the original order.

                • Then make a rectangular selection around the line numbers and delete them.

                Joel RodriguesJ samsung s6 LiteS 3 Replies Last reply Reply Quote 1
                • Joel RodriguesJ
                  Joel Rodrigues @Coises
                  last edited by

                  @Coises

                  WOW! Thank you so much!!!
                  It worked beautifully!

                  btw, I didn´t know your plugin so far. Thank you for sharing it for the public.

                  best regards

                  JR

                  1 Reply Last reply Reply Quote 0
                  • samsung s6 LiteS
                    samsung s6 Lite @Coises
                    last edited by

                    @Coises
                    Hi.
                    I have a similar issue but i need different help
                    I have these lines throughout my file, occurring between 300 to 400 times,

                    SnapRestSeconds = 76
                    SnapRestSeconds = 120

                    I would like to have the numbers replaced so its incremental and starting at number 30.
                    I can remove the numbers and replace them if necessary.

                    Is it possible to do this?
                    Any help would be appreciated.

                    1 Reply Last reply Reply Quote 0
                    • samsung s6 LiteS
                      samsung s6 Lite @Coises
                      last edited by

                      @Coises
                      UPDATE FROM MY PREVIOUS MESSAGE

                      Hi.
                      using your method I managed to work out how to do it PARTIALLY using this.

                      Find what: SnapRestSeconds = \d+
                      Replace with: SnapRestSeconds = (?=match)
                      Search mode: Regular expression

                      This starts them at number at ‘2’ I need it to start at 30 please, can you help?

                      CoisesC 1 Reply Last reply Reply Quote 0
                      • CoisesC
                        Coises @samsung s6 Lite
                        last edited by

                        @samsung-s6-Lite said in Help REPLACING content with incremental order (by +1):

                        using your method I managed to work out how to do it PARTIALLY using this.

                        Find what: SnapRestSeconds = \d+
                        Replace with: SnapRestSeconds = (?=match)
                        Search mode: Regular expression

                        This starts them at number at ‘2’ I need it to start at 30 please, can you help?

                        If it started at 2, then you did one sequential Replace with the same replace expression first. That’s OK — just be aware that replacing “continues from where you left off” if the expression hasn’t changed. After you close and reopen the Replace in indicated region dialog, change the expression, do a Replace All or select Clear History from the drop-down menu beside Replace All, counting will start again from 1.

                        The expression you want is (?=match+29). (Since match will start from one, add 29 to match to start from 30.)

                        1 Reply Last reply Reply Quote 2
                        • First post
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors