Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to automate a series of search and replace operation in Notepad++

Tags:

notepad++

I am having a file in which there are number of lines which i have to remove. These lines can be distinguished using regular expression and thus utilizing series of search and replace i am able to remove them.

Now i have to repeat the same task a number of times. So i am looking for a way that i don't need to go through this series of replace again & again. Is there any way to batch/script these series of replace into a single command.

I tried doing so by recording a macro, but does not seems to work.

like image 898
Amit Avatar asked Oct 24 '22 03:10

Amit


2 Answers

Try the Python Script plug-in, if you're familiar with Python. If you're not familiar with Python, you will be it in no time. Doing such kinds of automation is a fingersnip with that plug-in. And if you can't get something to work, you'll find help at the support forum of that plug-in.

At least one example link about regex replacement via the plug-in.

Besides I'm using replacement macros in N++ myself. Did you try with the complex singletabbed "Find/Replace" dialog or with the simple "Replace" tab of the multitabbed find dialog, which also supports regex? The latter one works for me. I didn't try the 1st one tho.

like image 93
ufo Avatar answered Oct 31 '22 12:10

ufo


Sample script Multiples_SR.py (see discussion) from Python Script plugin sample scripts collection does what you need.

From the explanation of the script:

This script :

  • Reads an existing "SR_List.txt" file, of the CURRENT directory, containing a list of SEARCH/REPLACEMENT strings, ONE PER line
  • Selects, one at a time, a COUPLE of SEARCH and REPLACEMENT regexes/expressions/strings/characters
  • Executes this present S/R on CURRENT edited file, in NOTEPAD++
  • Loop till the END of the file

Any PURE BLANK line or COMMENT line, beginning with '#', of the "SR_list.txt" file, is simply IGNORED

For EACH line, in the "SR_List.txt" file, the format is

<DELIMITER><SEARCH regex><DELIMITER><REPLACE regex><DELIMITER>
like image 36
Boris Alekseev Avatar answered Oct 31 '22 13:10

Boris Alekseev