• Login
Community
  • Login

How to replace multiple "variables" vs a template

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
4 Posts 4 Posters 2.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.
  • A
    Andrea Zaccheroni
    last edited by Mar 3, 2022, 2:04 PM

    I’ve a certain configuration template i have to replicate N times where N is the number of lines in a file replacing certain “words” with the ones present in the source line, a sort of mail merge I’d like to obtain in NPP… Hope the example is more clear:
    Template text/file:

    conf conf %VAR1% conf
    conf  %VAR1% conf %VAR1%
    conf conf %VAR1% conf conf %VAR2%
    conf %VAR2%
    

    file with list of variables:

    *var1*,*var2*
    *test,*merge*
    *mail*,*MERGE*
    

    desired result:

    conf conf *var1* conf
    conf  *var1* conf *var1*
    conf conf *var1* conf conf *var2*
    conf *var2*
    conf conf *test* conf
    conf  *test* conf *test*
    conf conf *test* conf conf *merge*
    conf *merge*
    conf conf *mail* conf
    conf  *mail* conf *mail*
    conf conf *mail* conf conf *MERGE*
    conf *MERGE*
    
    P 1 Reply Last reply Mar 3, 2022, 5:55 PM Reply Quote 0
    • P
      PeterJones @Andrea Zaccheroni
      last edited by PeterJones Mar 3, 2022, 6:16 PM Mar 3, 2022, 5:55 PM

      @andrea-zaccheroni ,

      Normally I would say that mail merge is something that should be done with a purpose-built tool (and I’m surprised no one has written a mailmerge plugin, that I can find, for Notepad++). But your example is easy enough (and since you went to the trouble to actually present your question with the data marked up, I am happy to help).

      I was recently reminded of the ToolBucket plugin (Plugins > Plugins Admin, checkmark ToolBucket, and Install), which has a multi-line FIND and REPLACE box, which makes search/replace like this much easier.

      1. active file =
        *var1*,*var2*
        *test,*merge*
        *mail*,*MERGE*
        PETER,JONES
        
      2. Plugins > ToolBucket > Multiline Find and Replace
      3. FIND = (?-s)^(.*?),(.*)$
      4. REPLACE =
        conf conf $1 conf
        conf $1 conf $1
        conf conf $1 conf conf $2
        conf $2
        
        (you can just copy that from the forum and paste it in the box)
      5. checkmark the “Use regular expression” checkbox
      6. Replace and/or Replace All
        112a4e5c-9d27-4815-b243-d3bedafd209a-image.png 7. With my example, the results were
        conf conf *var1* conf
        conf *var1* conf *var1*
        conf conf *var1* conf conf *var2*
        conf *var2*
        conf conf *test conf
        conf *test conf *test
        conf conf *test conf conf *merge*
        conf *merge*
        conf conf *mail* conf
        conf *mail* conf *mail*
        conf conf *mail* conf conf *MERGE*
        conf *MERGE*
        conf conf PETER conf
        conf PETER conf PETER
        conf conf PETER conf conf JONES
        conf JONES
        

      I think that does what you want.

      ----

      Useful References

      • Please Read Before Posting
      • Template for Search/Replace Questions
      • FAQ: Where to find regular expressions (regex) documentation
      • Notepad++ Online User Manual: Searching/Regex
      M 1 Reply Last reply Mar 4, 2022, 5:35 PM Reply Quote 4
      • M
        Michael Vincent @PeterJones
        last edited by Mar 4, 2022, 5:35 PM

        @peterjones said in How to replace multiple "variables" vs a template:

        I think that does what you want.

        You could do this with Notepad++ regular expression Find/Replace, you just need to put the newlines in the “Replace” text box:

        conf conf $1 conf\nconf $1 conf $1\nconf conf $1 conf conf $2\nconf $2
        

        In case you don’t want (or can’t install) the plugin. The plugin text box does make it much more obvious, intuitive, visually appealing for the multi-line search and replace.

        Cheers.

        A 1 Reply Last reply Mar 4, 2022, 6:01 PM Reply Quote 2
        • A
          Alan Kilborn @Michael Vincent
          last edited by Mar 4, 2022, 6:01 PM

          @michael-vincent said in How to replace multiple "variables" vs a template:

          conf\nconf

          And you’d want to be careful about what type of “newlines” you put in; specifically on Windows you are likely to want \r\n instead of \n.

          1 Reply Last reply Reply Quote 3
          • P PeterJones referenced this topic on Oct 22, 2022, 5:23 PM
          • P PeterJones referenced this topic on Jan 24, 2023, 5:48 PM
          • M mkupper referenced this topic on Jan 17, 2024, 8:39 PM
          4 out of 4
          • First post
            4/4
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors