• Login
Community
  • Login

Find-Replace where Replacing with a Variable Help

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
12 Posts 3 Posters 7.8k 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.
  • T
    Tony Monroe
    last edited by Mar 17, 2016, 1:29 PM

    Hi all. First post on this forum but I need a little help. I’m not entirely sure if this is possible using regular expressions but I thought this would be a good place to ask and, even if it is impossible, point me in the direction of a plugin or tool that could do this.

    Basically I have a text file defining a number of records with different fields like so…
    record1 = {
    | | | | field1: value
    | | | | field2: value
    }
    record2 = {
    … etc

    I want to find every one of a particular field and replace the value based on which record it appears in. For instance, record1.field2 = 1, record2.field2 = 1.1, record3.field2 = 1.2 etc. etc.

    All records are sequential if that makes it easier.

    Failing that, if it was possible to isolate the value and apply a multiplier to it, that will also help.

    I’m not too sure if this is possible using the built-in Find-Replace feature in Notepad++ but I understand it’s a very powerful little tool and there’s a lot in it which I don’t know about. So any help would be appreciated. :)

    C 1 Reply Last reply Mar 17, 2016, 8:07 PM Reply Quote 0
    • C
      Claudia Frank @Tony Monroe
      last edited by Mar 17, 2016, 8:07 PM

      Hello @Tony-Monroe,

      can we safely assume that the number part of record is increasing by 1 always?
      Is field2 always the name field2 or is it just a reference of being the second field?

      I would assume that a python script can solve this. Assuming that python script plugin will/has be(en) installed.

      Cheers
      Claudia

      1 Reply Last reply Reply Quote 0
      • T
        Tony Monroe
        last edited by Mar 18, 2016, 3:47 PM

        Yeah a python script would be a good idea. I’ll need to brush off my python books though. Any good tutorials for using python with Np++ off the top of your head? I only script with Lua really, and first programming language was C, so I kinda skipped python.

        The records aren’t actually named anything different. That was just for the example. So it would have to be based on the line number or order it was found.

        1 Reply Last reply Reply Quote 0
        • C
          Claudia Frank
          last edited by Mar 18, 2016, 4:45 PM

          If you are familiar with Lua I would propose you install Lua script plugin ,
          which wraps the scintilla a notepad functions. Currently it looks like the download is broken but I assume @dail will fix this soon.

          If python should be used anyway, then I would propose to install the msi and documentation is provided here .

          If there is any question in regards how to use or how to solve, let us know.

          Cheers
          Clauida

          1 Reply Last reply Reply Quote 0
          • D
            dail
            last edited by Mar 18, 2016, 5:06 PM

            What exactly is broken?

            1 Reply Last reply Reply Quote 0
            • C
              Claudia Frank
              last edited by Mar 18, 2016, 5:27 PM

              Hi dail,
              when trying to download the precompiled plugin you get an access denied message,
              Source can be downloaded.

              Cheers
              Claudia

              1 Reply Last reply Reply Quote 0
              • D
                dail
                last edited by Mar 18, 2016, 5:49 PM

                Well darn. Might be a few days till I can check into it.

                1 Reply Last reply Reply Quote 0
                • C
                  Claudia Frank
                  last edited by Mar 18, 2016, 7:55 PM

                  Hi dail and all,

                  it is working - problem was my firefox configuration which doesn’t allow cross site scripting.
                  Once I allowed github-cloud.s3.amazonaws.com , dll could be downloaded. Sorry for confusing.

                  Cheers
                  Claudia

                  1 Reply Last reply Reply Quote 0
                  • T
                    Tony Monroe
                    last edited by Mar 19, 2016, 8:18 AM

                    Well thanks all for the advice. I chose to try scripting it in Python because I’m always keen to learn something new and Python seems nice and easy for such a simple job. Somewhat expectedly my first attempt hasn’t worked. I’m not entirely sure I’m using the functions right and I don’t want anybody to write the script for me but any help pointing me in the right direction would be appreciated. It’s very self-explanatory - the script finds the specified field and changes its value and adding an ‘f’ at the end to indicate a float to the parser. It skips a number of records at the beginning of the file signified by ignore_records, setting those to just initial_value without adding on delta_value. When I run it however, I get a notification saying “Unknown exception” and nothing happens.

                    field = “foo”
                    initial_value = 1
                    delta_value = 0.05
                    ignore_records = 3
                    record_n = 0

                    Editor.gotoLine(0)
                    Editor.searchAnchor()

                    while Editor.gotoPos( Editor.searchNext(0,field) ) != -1
                    record_n += 1
                    if record_n > ignore_records
                    initial_value += delta_value
                    Editor.setSel( Editor.getAnchor + len(field), Editor.getLineEndPosition() )
                    Editor.replaceSel( str(initial_value) + ‘f’ )

                    1 Reply Last reply Reply Quote 0
                    • C
                      Claudia Frank
                      last edited by Mar 19, 2016, 1:21 PM

                      The “Unknown exception” error indicates that something went wrong during install of the plugin.
                      Could it be, that you used the plugin manager instead of the msi package?

                      Regarding the script, use the console (Plugins->Python Script->Show console) to get the errors and help.
                      If you type help(name_of_the_function) like help(editor.gotoPos) into the >>> inputbox,
                      you will get additional informations about that function and you can use it to test a particular function.
                      Btw. python is case sensitive.

                      Cheers
                      Claudia

                      1 Reply Last reply Reply Quote 0
                      • T
                        Tony Monroe
                        last edited by Mar 19, 2016, 3:17 PM

                        Thanks all. It’s not very elegant but I got it working. :)

                        field = “foo =”
                        initial_value = 1
                        delta_value = 0.05
                        ignore_records = 3
                        record_n = 0

                        editor.gotoLine(0)
                        editor.searchAnchor()

                        while editor.searchNext(0,field) != -1 :
                        editor.gotoPos( editor.searchNext(0,field) )
                        record_n += 1
                        if record_n > ignore_records :
                        initial_value += delta_value
                        editor.setSel( editor.getAnchor() + len(field), editor.getLineEndPosition(editor.lineFromPosition(editor.getCurrentPos())) )
                        editor.replaceSel( str(initial_value) + ‘f’ )
                        editor.searchAnchor()

                        1 Reply Last reply Reply Quote 0
                        • C
                          Claudia Frank
                          last edited by Mar 19, 2016, 4:42 PM

                          That’s what it is all about - get it working.
                          One thing which makes me wonder is that record_n doesn’t get reset,
                          so after 3rd run

                          if record_n > ignore_records :
                          

                          is always true.

                          Btw. if you post code, intend the whole code by 4 spaces then it keeps the layout ;-)

                          Cheers
                          Claudia

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