• Login
Community
  • Login

Find Replace <filename000> with <filename001>

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
9 Posts 6 Posters 550 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.
  • R
    reddreddyredd
    last edited by reddreddyredd Jun 5, 2023, 4:36 AM Jun 5, 2023, 4:26 AM

    Hello, hopefully there is an easy way to do this. I have a large snippet of code which references to a file name called:fire_smoke_multi000

    this file is listed in this snippet of code 120 times. Starting from 0 and ending at 119.

    Is there an easy way to find every instance fire_smoke_multi000 and replace it with it’s proper numbering like so:
    fire_smoke_multi000
    fire_smoke_multi001
    fire_smoke_multi002
    fire_smoke_multi003
    all the way up to… fire_smoke_multi119

    Essentially it finds the first instance, numbers it 000 then finds the next instance and numbers it 001, then the next instance 002, and so on. All the way up to 119.

    Has to be a way to essentially replace the “000” with some sort of XXX value number starting at 000 and going up to 119.

    M L 2 Replies Last reply Jun 5, 2023, 6:26 AM Reply Quote 0
    • M
      Mark Olson @reddreddyredd
      last edited by Jun 5, 2023, 6:26 AM

      @reddreddyredd

      1. Find/replace 000$ with nothing (regular expressions on). This will remove the final 000 from the line.
      2. Move your cursor to the last column of the first line (right after the i in fire_smoke_multi).
      3. Open the Column/Multi-Selection Editor (with default keybindings, can be accessed with Alt-C). Use the settings shown in the below picture.

      4463e44f-7946-4278-99ac-71f921a9d0a1-image.png

      R 1 Reply Last reply Jun 5, 2023, 8:22 PM Reply Quote 2
      • R
        reddreddyredd @Mark Olson
        last edited by Jun 5, 2023, 8:22 PM

        @Mark-Olson The issue I have though is that this is not in a simple list. I have code scattered across the whole page, so I can’t use the column option I don’t think as the code aligns with other code.

        T C 2 Replies Last reply Jun 5, 2023, 8:36 PM Reply Quote 0
        • T
          Terry R @reddreddyredd
          last edited by Terry R Jun 5, 2023, 8:48 PM Jun 5, 2023, 8:36 PM

          @reddreddyredd said in Find Replace <filename000> with <filename001>:

          I have code scattered across the whole page,

          There is a way to do it if ONLY 1 “file_smoke_multi” exists on any one line.

          The steps are:

          1. Number all the lines in the file using Edit, Column Editor, Number to insert increasing by 1. Essentially you would be creating a line number at the start of the line.
          2. Mark each line which contains the relevant string using Mark with “bookmark line” ticked.
          3. Cut these lines out and insert into another temporary tab.
          4. Create new line numbers, these will be the numbers you really want behind the string.
          5. Use a regular expression to move these to the correct position.
          6. Copy all the lines back to the original file and re-sort numerically.
          7. Remove the line numbers.

          Terry

          PS I think I may have provided this solution some time ago. A bit of searching may find the expanded solution. If I find it I will post the link.
          All I could find was this thread, which is much the same, although also has other links to using a pythonscript solution.

          1 Reply Last reply Reply Quote 3
          • M
            Mark Olson
            last edited by Mark Olson Jun 5, 2023, 8:46 PM Jun 5, 2023, 8:40 PM

            TerryR’s solution should work fine.

            PythonScript solution:

            from Npp import *
            
            ct = 0
            
            def on_match(m):
                global ct
                out = '%s%03d' % (m.group(0), ct)
                ct += 1
                return out
            
            editor.rereplace('(file_smoke_multi)', on_match)
            

            This converted

            file_smoke_multi
            foo file_smoke_multi
            bar file_smoke_moola
            baz
            file_smoke_munky
            file_smoke_multi
            

            to

            file_smoke_multi000
            foo file_smoke_multi001
            bar file_smoke_moola
            baz
            file_smoke_munky
            file_smoke_multi002
            
            1 Reply Last reply Reply Quote 3
            • L
              Lycan Thrope @reddreddyredd
              last edited by Lycan Thrope Jun 5, 2023, 8:59 PM Jun 5, 2023, 8:41 PM

              @reddreddyredd ,
              If the answers you’ve received so far aren’t sufficient, then the answer is no, unless you write your own script with PythonScript, or make your own plugin.

              1 Reply Last reply Reply Quote 2
              • C
                Coises @reddreddyredd
                last edited by Coises Jun 5, 2023, 10:13 PM Jun 5, 2023, 10:05 PM

                @reddreddyredd said in Find Replace <filename000> with <filename001>:

                @Mark-Olson The issue I have though is that this is not in a simple list. I have code scattered across the whole page, so I can’t use the column option I don’t think as the code aligns with other code.

                Can you determine some single character which does not exist anywhere in your file?
                If you can, let’s call that character # (but use whatever you determine). Also, I’ll assume Windows line ending conventions, but change that as needed. (If you can’t find a single character, a sequence of characters will work just the same, so long as it doesn’t occur anywhere in your file.)

                First, use Replace to change each occurrence of \r\n to #.
                Now you have one single line with # where the line breaks belong.

                Next, change each occurrence of fire_smoke_multi to \r\nfire_smoke_multi.
                Now you have each occurrence of fire_smoke_multi at the beginning of a line.

                Use column mode replacement to replace the numbers. Then replace \r\n with null, then replace # with \r\n.

                R 1 Reply Last reply Jun 5, 2023, 11:55 PM Reply Quote 2
                • R
                  reddreddyredd @Coises
                  last edited by Jun 5, 2023, 11:55 PM

                  @Coises said in Find Replace <filename000> with <filename001>:

                  \r\n

                  THIS WORKED! Using your method combined with Terry’s I was able to follow it easily enough and make the changes. Thanks dudes! You rock! Will remember this for the future for sure!

                  1 Reply Last reply Reply Quote 0
                  • G
                    guy038
                    last edited by Jun 6, 2023, 3:48 AM

                    Hello, @reddreddyredd, @mark-olson, @terry-r, @lycan-thrope, @coises and All,

                    @reddreddyredd, I know that you already reached your goal, with all the advices given in the previous posts, but here is an alternate method :


                    • Move the caret at the beginning of your first string fire_smoke_multi000

                    • Automatically, place any instance of fire_smoke_multi000 at the very beginning of lines with the following regex S/R :

                      • SEARCH \R(?!fire_smoke_multi000)

                      • REPLACE #

                    • Do a 120 × 3 rectangular selection of the string 000

                    • Open the **Column Editor ( ALT + C ) and replace this selection with the appropriate numbering

                    • Finally, replace any # character with a line-break, with the regex S/R :

                      • SEARCH #

                      • REPLACE \r\n    OR    \n ( for an UNIX file )

                    Best Regards,

                    guy038

                    1 Reply Last reply Reply Quote 0
                    4 out of 9
                    • First post
                      4/9
                      Last post
                    The Community of users of the Notepad++ text editor.
                    Powered by NodeBB | Contributors