• Login
Community
  • Login

Adjust Numbers in an expression by a set amount

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
16 Posts 3 Posters 1.2k 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.
  • L
    Leon Bob Noël
    last edited by Jun 1, 2021, 3:24 AM

    Re: Replacing number with another Incrementing #

    I am trying to adjust some subtitle files, so I have about 60 files that all need the same adjustments. The problem is that it’s not all the same numbers.
    For example,

    00:00:36.250 line:77%
    00:00:36.250 line:84%
    00:00:42.333 line:85%
    00:00:47.458 line:84%
    

    I need to move everything down by 5% so all the 84% should be 89%, all the 85% should be 90%, all the 77% should be 82%, etc. The 84, 85, 76, & 77 are common enough that I could do a general search for all of them, but every subtitle is just slightly off of where it should be. the ones that are a problem are the random ones at 13% or 24% or 35%. I saw another post that had a somewhat similar issue that was a bit different, so I figured I would see if someone knows something that’d help.

    https://community.notepad-plus-plus.org/topic/19345/replacing-number-with-another-incrementing/18

    E 1 Reply Last reply Jun 1, 2021, 9:10 AM Reply Quote 0
    • E
      Ekopalypse @Leon Bob Noël
      last edited by Jun 1, 2021, 9:10 AM

      @Leon-Bob-Noël

      I don’t know exactly what you are going to do now.
      Do you want to increase all percentages by 5 or not?
      What if values are greater than or equal to 96%, do you want to set them to 100%? Or is there an upper limit?

      1 Reply Last reply Reply Quote 1
      • L
        Leon Bob Noël
        last edited by Jun 1, 2021, 8:42 PM

        So I’m trying to increase the values by 5%. 100% is the upper limit, but 87% is the highest I’ve seen. The ones at the bottom 84, 85, with a couple at 86 & 87 I’m completely removing the line:84% value entirely because I want those to be at the bottom. So I’m not worried about it hitting the upper limit. Oh, yeah, so It does % by % down from the top, so when i say down that’s a higher number, sorry about that.
        I want every “line:??%” value to increase by +5% of whatever is there. if there happens to be something that goes over 100% it will be ignored by VTT anyways. I’ve done for 1 folder doing a [Find In Files] starting at 99% & going down increasing everything by 5%, but it took a lot of time & I have to repeat the process for other shows when I find the appropriate % I need to shift. I figure there has to be a simpler way to do it.
        Thanks for the help

        E 1 Reply Last reply Jun 1, 2021, 9:15 PM Reply Quote 0
        • E
          Ekopalypse @Leon Bob Noël
          last edited by Jun 1, 2021, 9:15 PM

          @Leon-Bob-Noël

          I assume that this python script will do what you want to do

          from Npp import editor
          
          def increase(m):
              current_value = int(m.group(0))
              current_value = current_value+5 if current_value < 96 else 100
              return  current_value
          
          editor.rereplace('(?<=line:)\d+(?=%)', increase)
          
          1 Reply Last reply Reply Quote 3
          • L
            Leon Bob Noël
            last edited by Jun 2, 2021, 3:07 AM

            @Ekopalypse said in Adjust Numbers in an expression by a set amount:

            def increase(m):
            current_value = int(m.group(0))
            current_value = current_value+5 if current_value < 96 else 100
            return current_value

            editor.rereplace(‘(?<=line:)\d+(?=%)’, increase)

            Awesome, it shifts the lines perfectly. But is there a way to make it work on more than 1 file at a time? I have folders of from 12 to 300 files that I need to do the same thing on. It’s a great tool to have on hand that I can adjust to whatever I need, but having to be doing it 60 times is still a problem. If it’s not possible it’s not possible, but I figured there has to be a way

            E 1 Reply Last reply Jun 2, 2021, 10:49 AM Reply Quote 0
            • E
              Ekopalypse @Leon Bob Noël
              last edited by Ekopalypse Jun 2, 2021, 10:50 AM Jun 2, 2021, 10:49 AM

              @Leon-Bob-Noël

              One solution could be to use something like this

              import os
              from Npp import editor
              
              
              def increase(m):
                  # As long as only one value in a line is changed
                  # one can set the default bookmark symbol to see where the changes took place
                  
                  # start, end = m.span(0)
                  # start_line = editor.lineFromPosition(start)
                  # end_line = editor.lineFromPosition(end)
                  # for i in range(start_line, end_line+1):
                      # editor.markerAdd(i, 24)
              
                  current_value = int(m.group(0))
                  current_value = current_value+5 if current_value < 96 else 100
                  return  current_value
              
              
              for currrent_directory, _, files in os.walk(r'PATH_WITHOUT_TRAILING_BAKSLASH'):
                  for _file in files:
                      if _file.endswith('.txt'):
                          notepad.open(os.path.join(currrent_directory, _file))
                          editor.rereplace('(?<=line:)\d+(?=%)', increase)
                          # notepad.save() # automatically save changes
                          # notepad.close() # close active buffer
              
              1 Reply Last reply Reply Quote 3
              • E
                Ekopalypse
                last edited by Jun 2, 2021, 10:53 AM

                It walks down from a given directory and loads the file with the txt extension and changes its contents if it matches something.

                L 1 Reply Last reply Jun 2, 2021, 8:38 PM Reply Quote 0
                • L
                  Leon Bob Noël @Ekopalypse
                  last edited by Jun 2, 2021, 8:38 PM

                  @Ekopalypse Would this work if the extension is VTT? It’s set to open as a text document but I don’t think that counts

                  A 1 Reply Last reply Jun 2, 2021, 8:47 PM Reply Quote 0
                  • A
                    Alan Kilborn @Leon Bob Noël
                    last edited by Jun 2, 2021, 8:47 PM

                    @Leon-Bob-Noël said in Adjust Numbers in an expression by a set amount:

                    Would this work if the extension is VTT?

                    Not as it is. But, really, is it that hard to see it?
                    I mean, the relevant line in the code reads almost like English:

                    if _file.endswith('.txt'):

                    Exercise left to the reader to figure out what to change it to. :-)

                    L 1 Reply Last reply Jun 3, 2021, 5:58 PM Reply Quote 1
                    • L
                      Leon Bob Noël @Alan Kilborn
                      last edited by Jun 3, 2021, 5:58 PM

                      @Alan-Kilborn oh yeah, cool. So do I not run this whithin Notepad ++ then?

                      1 Reply Last reply Reply Quote 0
                      • L
                        Leon Bob Noël
                        last edited by Jun 3, 2021, 6:17 PM

                        I’ve tried running it in Notepad ++ python scripts but no matter how I do it it does not work. With a file open in the directory it does nothing, with the directory open as workspace it does nothing, I didn’t try it with those setting with TXT files, assuming that changing it to “if _file.endswith(‘.vtt’)::” didn’t work because Notepad++ wasn’t recognizing them because it doesn’t recognize the extension by default & doesn’t have the language in it’s database

                        1 Reply Last reply Reply Quote 0
                        • E
                          Ekopalypse
                          last edited by Ekopalypse Jun 3, 2021, 6:27 PM Jun 3, 2021, 6:26 PM

                          @Leon-Bob-Noël
                          It must run from within Npp but there is no need to load the file,
                          this is done by the script.
                          You just have to adjust the file ending and the root path.

                          os.walk(r'PATH_WITHOUT_TRAILING_BAKSLASH'):
                          

                          example

                          os.walk(r'C:\Documents\whatever\directory\it\is'):
                          
                          L 1 Reply Last reply Jun 3, 2021, 10:27 PM Reply Quote 1
                          • L
                            Leon Bob Noël @Ekopalypse
                            last edited by Leon Bob Noël Jun 3, 2021, 10:28 PM Jun 3, 2021, 10:27 PM

                            @Ekopalypse Ah, thanks. Will is do recursive directories or just the root specified?

                            E A 2 Replies Last reply Jun 4, 2021, 10:35 AM Reply Quote 0
                            • E
                              Ekopalypse @Leon Bob Noël
                              last edited by Jun 4, 2021, 10:35 AM

                              @Leon-Bob-Noël - it walks the tree recursively.

                              1 Reply Last reply Reply Quote 1
                              • A
                                Alan Kilborn @Leon Bob Noël
                                last edited by Jun 4, 2021, 11:36 AM

                                @Leon-Bob-Noël

                                You could just experiment with it to find out these answers, instead of asking and then waiting to have a response.

                                L 1 Reply Last reply Jun 10, 2021, 8:11 AM Reply Quote 2
                                • L
                                  Leon Bob Noël @Alan Kilborn
                                  last edited by Jun 10, 2021, 8:11 AM

                                  @Alan-Kilborn if I was receiving answers while I was able to actually do it sure, but then if not I’d have to then ask if there was a way to do it. That seems much less efficient. I know when I’m helping someone I’d much rather have them ask questions right away. The longer they wait the less likely I’ll be looking at the forum to know they need a response. Maybe that’s just me, but having a question & waiting to ask it because it might not be necessary is just rude to the person giving help, while asking questions that are unnecessary right away is only really harmful to the ego of the asker, I’d rather look like I don’t know something I actually don’t know, than go to do something & have to come back & ask for more information because I was afraid of looking like an idiot, & thereby actually being an idiot

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