Author Topic: Batch find and replace using a list (match whole word, match case, regular expression, regex)  (Read 256 times)

spiros

  • Administrator
  • Hero Member
  • *****
  • Posts: 296449
  • Gender: Male
  • point d’amour
    • spiros.doikas
    • greektranslator
    • doikas
    • 102094522373850556729
    • lavagraph
    • Greek translator CV
Batch find and replace using a list

Let's say you have a list with words you want to replace (it could be many thousands of different replacements) and a text in which the find/replace operation will be run. There are some programs who support that.

Method 1
ASAP Utilities (ASAP Utilities > ASAP Utilities Options > New tools in development > Bulk replace: replace multiple values at once). No option to match whole words.

Method 2
EmEditor > Ctrl+H to bring up the Find/Replace dialog. Then use Import to import your list (it has to be a four-column tab delimited file with a .tsv extension). Your replacement file must be encoded in UTF-16LE with Signature if it contains non-Western scripts. The tsv format needs to be in four columns, ("CW" at the end means case sensitive and match whole word, of course you may have one or the other or none) and the extension has to be tsv:

on[tab]findword[tab]replaceword[tab]CW

The easiest way to figure out the correct format is to manually add a replacement to your liking (i.e. maybe add regex too) with the Add to Batch button. Once you do that, you can Export the file to see the format and replicate for your other entries (see screenshot below and the EmEditor link above).

Method 3
Notepad++. Plugins > Plugin Manager > Show Plugin Manager > Available, find Python Script, add a check mark next to it and Install.
Then Plugins > Python Script > New Script, save your file with a name of your liking. Open the file with Notepad++ and paste this code:

Code: [Select]
with open('C:/fix.txt') as f:
    for l in f:
        s = l.split()
        editor.rereplace(r'\b' + s[0] + r'\b', s[1])

Change the path of C:/fix.txt to your replacements file (it has to be a two column file delimited with space). The match will be on a whole word basis, i.e. it will not match the "new" bit in "anew" for the entry "new". If you do not want whole word matching, then change the last line to:

Code: [Select]
editor.replace(s[0], s[1])
Open the text file in which you want to run the replacements and run the path script, Notepad++ > Plugins > Python Script > Scripts > select the name of your script.
« Last Edit: 14 Jul, 2017, 09:53:12 by spiros »