Community
    • Login

    Find Replace <filename000> with <filename001>

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    9 Posts 6 Posters 471 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.
    • reddreddyreddR
      reddreddyredd
      last edited by reddreddyredd

      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.

      Mark OlsonM Lycan ThropeL 2 Replies Last reply Reply Quote 0
      • Mark OlsonM
        Mark Olson @reddreddyredd
        last edited by

        @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

        reddreddyreddR 1 Reply Last reply Reply Quote 2
        • reddreddyreddR
          reddreddyredd @Mark Olson
          last edited by

          @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.

          Terry RT CoisesC 2 Replies Last reply Reply Quote 0
          • Terry RT
            Terry R @reddreddyredd
            last edited by Terry R

            @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
            • Mark OlsonM
              Mark Olson
              last edited by Mark Olson

              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
              • Lycan ThropeL
                Lycan Thrope @reddreddyredd
                last edited by Lycan Thrope

                @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
                • CoisesC
                  Coises @reddreddyredd
                  last edited by Coises

                  @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.

                  reddreddyreddR 1 Reply Last reply Reply Quote 2
                  • reddreddyreddR
                    reddreddyredd @Coises
                    last edited by

                    @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
                    • guy038G
                      guy038
                      last edited by

                      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
                      • First post
                        Last post
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors