Home > regexplace

regexplace

Regexplace is a project mainly written in Python, it's free.

A simple tool to replace arbitary text within multiple files.

Regexplace

Regexplace is a simple tool to change multiple files within one directory structure. The idea is to search for files and replace within all this files a given string multiple times. On Linux there are multpiple tools that can be used to achieve this but since I needed that on Windows as well. I hacked/copied this tool together. I haven't used it on Windows for a few years now but it should still work.

The original script was from the http://aspn.activestate.com/ASPN/Cookbook/Python[Activestate Python Cookbok]. Some times it is necessary to change an entry in more then one file. I made some small changes to the original script to get it easier to use.

Future Ideas


A Simple gui which lets you enter the text to search and replace and some
simple file selection could help to make this tool a little helper without the
need to learn some fancy shell commands.

Usage of the Script

usage: regexplace.py [options] dirname search-regexp replace-string

options: --version show program's version number and exit -h, --help show this help message and exit -f FILEREGEX, --files=FILEREGEX set the regular expression of files to search through. The default vaule is: '.' (All files) -i IGNOREFILEREGEX, --ignorefiles=IGNOREFILEREGEX set the regualr expression for the files to ignore. This parameter applies as well to the directory on the recursive search. The default value is: '..' (Ignore files with leading dot) -r, --recursive Prse files in subdirectories recursively.

Examples



A simple example where I want to change the text TEST in all the file in the
directory /home/user/test to ERROR. I could call the script as follow:

----
$ python regexplace.py /home/user/test TEST ERROR
----

This will change all the files in the directory test but not in the
subdirectories for this it would be necessary to add the -r switch.

----
$ python regexplace.py -r /home/user/test TEST ERROR
----

If I only want to change the .cpp and .h files. I would add the following regex
for the files.

----
$ python regexplace.py -r -f "(.*\.cpp)|(.*\.h)" /home/user/test TEST ERROR
----

If I want to change all files but not the files in the bin directory I add a
regex for this directory. But i will leave the rule for ignoring dot files.

----
$ python regexplace.py -r -f "(.*\.cpp)|(.*\.h)" -i "(\..*)|(bin)" /home/user/test TEST ERROR
----

For a description of the the regex syntax see the 
http://docs.python.org/lib/re-syntax.html[python documentation].
Previous:ROAMR