Community
    • Login

    How to replace intrementally?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    25 Posts 4 Posters 2.4k 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.
    • EkopalypseE
      Ekopalypse @Levente Horváth
      last edited by Ekopalypse

      @Levente-Horváth

      No, you do not have it selected, you just highlighted it, this is different and no, regex is not a calculator.
      If you have to do such things more often then I would recommend to
      install one of the scripting plugins like PythonScript, LuaScript and create
      a little script to do this for you.
      If you want to go that way we certainly can give you help solving this.

      Levente HorváthL 1 Reply Last reply Reply Quote 3
      • Alan KilbornA
        Alan Kilborn @Levente Horváth
        last edited by

        @Levente-Horváth said in How to replace intrementally?:

        Replace With: \d{10}+86400

        I thought @Ekopalypse makes it clear above that things DO NOT work that way.

        1 Reply Last reply Reply Quote 1
        • Levente HorváthL
          Levente Horváth @Ekopalypse
          last edited by Levente Horváth

          @Ekopalypse Then I will do it that way, whatever it takes.

          1 Reply Last reply Reply Quote 1
          • EkopalypseE
            Ekopalypse
            last edited by

            So you are going to install, e.g. PythonScript plugin via the plugin admin?
            If yes, what should the script do? Just add one day aka 86400 seconds?
            Or more flexible like you’ve been asked to enter the seconds you want to add?

            Levente HorváthL 1 Reply Last reply Reply Quote 0
            • Levente HorváthL
              Levente Horváth @Ekopalypse
              last edited by

              @Ekopalypse I just want to add 86400 to the existing numbers.

              1 Reply Last reply Reply Quote 0
              • EkopalypseE
                Ekopalypse
                last edited by

                ok, once you installed PythonScript, goto plugins->Python Script->New Script
                give it a meaningful name and save it.
                Paste the following into the newly create document and save it.

                from Npp import editor
                
                def add_a_day(m):
                    return int(m.group(0)) + 86400
                
                editor.rereplace('\d{10}', add_a_day)
                

                Goto to plugin->Python Script->Scripts and execute your newly created script.

                If you want to add it to the toolbar play with the configuration menu of pythonscript.

                Alan KilbornA 1 Reply Last reply Reply Quote 4
                • Alan KilbornA
                  Alan Kilborn @Ekopalypse
                  last edited by

                  @Ekopalypse said in How to replace intrementally?:

                  editor.rereplace(‘\d{10}’, add_a_day)

                  Perhaps the OP’s data contains other 10 digit numbers, not related to timestamps??

                  I might go with this instead:

                  editor.rereplace('"timestamp": \K\d{10}', add_a_day)

                  Ensures that we are only changing the timestamp values.

                  Levente HorváthL 1 Reply Last reply Reply Quote 3
                  • Levente HorváthL
                    Levente Horváth @Alan Kilborn
                    last edited by

                    @Alan-Kilborn No, it doesn’t contain any other 10 digit number, but I thank all of you, it works!

                    1 Reply Last reply Reply Quote 2
                    • guy038G
                      guy038
                      last edited by guy038

                      Hello, @levente-horváth, @ekopalypse, @alan-kilborn,

                      Let give it a try with the gawk utility !

                      The gawk utility reads each line of a file and splits it into different fields

                      • The option -F specifies the separator(s) of the fields. As the value of that option is : |, then, the separators are :

                        • A colon, followed with a space char
                          OR
                        • A comma

                      which defines, in specific lines "timestamp": 1588464052,, 3 different fields :

                      • Field $1 = The string "timestamp" preceded with 2 space chars

                      • Field $2 = The Unix timestamp 1588464052

                      • Field $3 = An EMPTY string


                      Now, it’s child’s play !

                      • Create a new folder

                      • Download the gawk-5.0.1-w32-bin-zip archive from https://sourceforge.net/projects/ezwinports/files/ , in that new folder

                      • Double-click on the gawk-5.0.1-w32-bin-zip archive

                      • Double-click on the bin folder

                      • Extract, only, the 5 files gawk.exe, libgmp-10.dll, libmpfr-4.dll, libncurses5.dll and libreadline6.dll in this new folder

                      • Open a DOS console window

                      • Go to your gawk’s folder

                      • Run the following command :

                      gawk -F": |," "/timestamp/ {$2 = $2 + 86400} ; {print > \"Output.txt\"}" Input.txt

                      Notes :

                      • The file processed is the file Input.txt

                      • Inside the gawk program section ".........", any double quote " must be escaped with a \ symbol

                      • If current line contains the string timestamp, then the field 2’s value is increased by 86,400

                      • In all cases, the resulting entire current line is rewritten in file Output.txt

                      Best Regards,

                      guy038

                      Alan KilbornA 1 Reply Last reply Reply Quote 2
                      • Alan KilbornA
                        Alan Kilborn @guy038
                        last edited by

                        @guy038 said in How to replace intrementally?:

                        Let give it a try with the gawk utility !

                        Please, let’s not.
                        Only when there isn’t a reasonable way, within Notepad++.
                        And one of those has already been provided.

                        1 Reply Last reply Reply Quote 0
                        • guy038G
                          guy038
                          last edited by guy038

                          Hi, @alan-kilborn and All,

                          I do understand the logic of your reasoning, Alan. In my defense, as this calculation couldn’t be done, natively, from notepad++, I thought I was entitled to propose this solution.

                          After all, AWK is a scripting language just like Python and it is possible to run an Gwak/Awk program, just using the Run command of Notepad++ ;-))

                          For instance, using the example of my previous post :

                          • Hit the F5 key, from within N++

                          • Paste the command cmd /c <ABSOLUTE Path to your GAWK.exe>\gawk -F": |," "/timestamp/ {$2 = $2 + 86400} ; {print > \"Output.txt\"}" Input.txt in the input zone

                          • Click on the Run button

                          Note that, both, the Input.txt file processed and the Output.txt file created are located in the current Notepad++ folder, in case of portable installations of N++

                          Nevertheless, I readily admit that the Python plugin is closer to Notepad++ than the AWK program !

                          Cheers,

                          guy038

                          Alan KilbornA 1 Reply Last reply Reply Quote 0
                          • Alan KilbornA
                            Alan Kilborn @guy038
                            last edited by

                            @guy038 said in How to replace intrementally?:

                            I thought I was entitled to propose this solution.

                            Sorry for giving you a hard time about it. You are certainly entitled.

                            But, anyone here that has to install “something” additional to get to a solution is very likely better off just grabbing PythonScript. It’s easy to obtain (Plugins Admin), you don’t have to keep track of it separately (after install it will just “ride along” with Notepad++, and keep track of any scripts you use/write, and its a reasonably modern programming language. The same holds for LuaScript, I believe, although I am not a user of that.

                            Aside:
                            Just so you know, I hold no hatred of AWK.
                            There was a time before Python when I was into AWK.
                            This BOOK still adorns my bookshelves.

                            1 Reply Last reply Reply Quote 1
                            • guy038G
                              guy038
                              last edited by guy038

                              Hello, @alan-kilborn,

                              Of course, it’s been more than 25 years since I really practice AWK. It was on an HP-Unix server that would be laughed at, nowadays !

                              I have already praised, several times, the precision and power of regexes, which, in a few words, produce great effects !

                              For the same reasons, it seems that I am simply impressed by the AWK language ;-))

                              Cheers,

                              guy038

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