Batch replacement of string occurence by their replacement string



  • Hello All,
    I need to replace occurences of strings by their respective replacements strings.
    2 files are used : 1 with the text to be deal with (the ‘Text file’), the second with the pair of strings (let’s call it ‘parameters file’)

    parameters file contains:

    string1A (to be found)
    string1B (replacement for string 1A)
    string2A (to be found)
    string2B (replacement for string 2A)

    Text file contains:

    whateverwhateverstring1Awhatever
    whateverwhateverstring1Bwhatever
    whateverstring1Awhateverwhateverwhateverstring1Bwhatever

    there is no special character in front of string, nor a space and it can be within a sentence.

    I’m looking to have a kind of loop, looking for string1A and replacing it by string1B through the whole ‘Text file’,
    then passing to string2A, etc…

    I’ve tried through macro, but not yet perfect,
    any idea ?
    btw, thx to all for making NP++ so great !
    and thx in advance for your reply ^^



  • You will have to do them once at a time. and you probably know this but…
    searct> replace (or Ctrl+H)
    find what: string1A
    replace with: string1B

    then click replace all.
    then repeat for whatever other strings you need to.

    you want to call them from a text file, set out in a CSV as

    string1A,string1B
    string2A,string2B

    hopefully someone else can help but i have never seen this done within NP++



  • Thank you James,

    As I have like hundreds of replacement to do,
    the ‘one at the time’ concept is really a huge work.

    about the CSV, I made a piece of VBA giving me the couples of string, and I can arrange them in all format.

    so, if I understand correctly, the question is:
    how can I automate the process of search/replace.

    I’ll check, in parallel if anyone has an idea ^^



  • @laurent-guillemeau
    This type of task is easily handled with Python, PERL, AWK, or other such scripting languages.

    If you need to do things like this, or other things that seem a bit to complicated for search and replace, I recommend you learn one of these languages.

    I assume you don’t already know one, because, if you did, I’m pretty sure you would have used it and wouldn’t be asking on this forum! :-)



  • @laurent-guillemeau

    If it is acceptable to put your “parameter” data inside your “text file” (sure it is, temporarily, right before you do the replacement operation…) then regular expressions can likely help.

    Have a look at these postings for some inspiration/techniques…or maybe a nice soul will come along and give you exact detail on how to do your specific scenario:

    https://notepad-plus-plus.org/community/topic/14337/concatenate-corresponding-lines-from-two-files
    https://notepad-plus-plus.org/community/topic/14548/help-replacing
    https://notepad-plus-plus.org/community/topic/15333/help-multiple-replacement-vertical-text



  • @Jim-Dailey
    it is correct, I don’t program with those scripting languages.

    @Scott-sumner
    The postings proposed are really interesting. my situation seems to be a bit a mix of those posts.
    the last one,
    replacement vertical text
    is clearly close ! :) However, like the last episode of a nice TV serie, it ends on THE proposition that may solve everything, and stops… !

    I will continue to investigate, starting with those posts and nice Regex tutorial.

    Many thanks for your reply to both of you !



  • @laurent-guillemeau

    So I think maybe this thread is actually the best one…

    Here’s what I’d do:

    • Take the “parameters” list and put it in an empty editor tab:
    string1A
    string1B
    string2A
    string2B
    
    • Run this regex replacement on it:

    Find what zone: (\w+)\R(\w+)
    Replace with zone: \1-\2
    Search mode: Regular expression
    Wrap around: ticked
    Action: Replace All

    to obtain:

    string1A-string1B
    string2A-string2B
    
    • Now add your “text file” data below this, to obtain something like this (I made up some data like your small sample):
    string1A-string1B
    string2A-string2B
    
    whateverwhateverstring1Awhatever
    whateverwhateverstring1Awhatever
    whateverwhateverstring1Bwhatever
    whateverwhateverstring2Awhatever
    whateverwhateverstring1Bwhatever
    whateverwhateverstring1Awhatever
    whateverwhateverstring1Bwhatever
    whateverwhateverstring2Bwhatever
    whateverwhateverstring2Awhatever
    whateverwhateverstring2Bwhatever
    whateverstring1Awhateverwhateverwhateverstring1Bwhatever
    whateverstring2Awhateverwhateverwhateverstring2Bwhatever
    whateverwhateverstring2Awhatever
    whateverstring2Awhateverwhateverwhateverstring2Bwhatever
    whateverstring1Awhateverwhateverwhateverstring1Bwhatever
    whateverstring2Awhateverwhateverwhateverstring2Bwhatever
    whateverstring1Awhateverwhateverwhateverstring1Bwhatever
    whateverwhateverstring2Bwhatever
    
    • Now run the following regex replacement:

    Find what zone: (?-i)^((\w+)-(\w+)(?s).*?)\g{2}(?!-)
    Replace with zone: \1\3
    Search mode: Regular expression
    Wrap around: ticked
    Action: Replace All

    BUT…and this is from the other thread mentioned: “…verify that the A in Replace All gets underlined when you press and hold the Alt key with the Find window active. If that’s the case, go ahead and hold down Alt+A. This will perform multiple Replace-Alls in succession. Watch the status bar of the Find window until you see it say Replace All: 0 occurrences were replaced. Release the hold-down of Alt+A.”

    • For me this turns the data above into this, which seems to meet spec:
    string1A-string1B
    string2A-string2B
    
    whateverwhateverstring1Bwhatever
    whateverwhateverstring1Bwhatever
    whateverwhateverstring1Bwhatever
    whateverwhateverstring2Bwhatever
    whateverwhateverstring1Bwhatever
    whateverwhateverstring1Bwhatever
    whateverwhateverstring1Bwhatever
    whateverwhateverstring2Bwhatever
    whateverwhateverstring2Bwhatever
    whateverwhateverstring2Bwhatever
    whateverstring1Bwhateverwhateverwhateverstring1Bwhatever
    whateverstring2Bwhateverwhateverwhateverstring2Bwhatever
    whateverwhateverstring2Bwhatever
    whateverstring2Bwhateverwhateverwhateverstring2Bwhatever
    whateverstring1Bwhateverwhateverwhateverstring1Bwhatever
    whateverstring2Bwhateverwhateverwhateverstring2Bwhatever
    whateverstring1Bwhateverwhateverwhateverstring1Bwhatever
    whateverwhateverstring2Bwhatever
    


  • Hi, @laurent-guillemeau, @Scott-sumner and All,

    I was trying to find out some sophisticated regex, in order to improve the process by running the S/R only once, when suddenly, a very simple regex syntax came to my mind. As says the French proverb : " Searching noon, when it’s 2 p.m." ! Scott, do you guess it… ?

    SEARCH (?-i)(string1A)|(string2A)

    REPLACE (?1string1B)(?2string2B)

    Notes :

    • If you need a non-sensitive way of search, simply change the part (?-i) into (?i)

    • Of course, you may add as many expressions to search as you want to !. So, the regex S/R would follows, for n strings, the general syntax :

      • SEARCH (?-i)(string1A)|(string2A)|........|(stringnA)

      • REPLACE (?1string1B)(?2string2B)..........(?nstringnb)

    • If a prefix and/or a suffix exist(s) for all the replacement strings, the replacement regex becomes :

    REPLACE Prefix(?1string1B)(?2string2B)..........(?nstringnb)Suffix

    Cheers,

    guy038



  • @guy038

    Well, yes, that occurred to me as well, initially…but then I assumed that possibly the OP’s word list was going to be too long to fit the 2046 (or is it 2047) character limits of the Find what and/or Replace with fields. Of course, if it is too long, potentially it could be done in batches…


Log in to reply