How to replace multiple "variables" vs a template
-
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* -
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.- active file =
*var1*,*var2* *test,*merge* *mail*,*MERGE* PETER,JONES - Plugins > ToolBucket > Multiline Find and Replace
- FIND =
(?-s)^(.*?),(.*)$ - REPLACE =
(you can just copy that from the forum and paste it in the box)conf conf $1 conf conf $1 conf $1 conf conf $1 conf conf $2 conf $2 - checkmark the “Use regular expression” checkbox
- Replace and/or Replace All
7. With my example, the results wereconf 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
- active file =
-
@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 $2In 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.
-
@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\ninstead of\n. -
P PeterJones referenced this topic on
-
P PeterJones referenced this topic on
-
M 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