Community
    • Login

    How to replace multiple "variables" vs a template

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 4 Posters 3.9k Views 2 Watching
    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.
    • Andrea ZaccheroniA Offline
      Andrea Zaccheroni
      last edited by

      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*
      
      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP Offline
        PeterJones @Andrea Zaccheroni
        last edited by PeterJones

        @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
        Michael VincentM 1 Reply Last reply Reply Quote 4
        • Michael VincentM Offline
          Michael Vincent @PeterJones
          last edited by

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

          Alan KilbornA 1 Reply Last reply Reply Quote 2
          • Alan KilbornA Offline
            Alan Kilborn @Michael Vincent
            last edited by

            @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
            • PeterJonesP PeterJones referenced this topic on
            • PeterJonesP PeterJones referenced this topic on
            • mkupperM mkupper referenced this topic on

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors