Search and replace in multiple files


for Perl programmers.

Or at least you should know some perl.

Using this tool you can edit and change multiple files simultaneously. The most common usage is global search and replace, but the limits are only in your imagination.

The tool was designed to be as safe as possible and easy to use, easy to remember. As such a tool is not used every day the program does NOT accept command line parameters -- except -h for help and -u for undo. The program asks you the questions, what to perform, therefore it is safe to start it just typing the name, like

perl prep.pl
The program will display the startup message
Multi Line Perl Execution Program V2.0
Use -h for help.
and ask you the following questions:
Start directory? [.]
File pattern:
Recurse subdirectories? [n]
Save undo information to file [none]?
Verbose mode [y]:
Keep original file times? [n]
Command to execute for each line:
The processing will cover all the files that match the file pattern that you specify in the directory you specify recursively down to the deepest directory if you request it.

The undo information saves appropriate undo information in case you mistyped something. You better do specify an undo file, like undo.txt. It takes almost no time and may save your life.

You should give a one line Perl command that will be eval-ed for each line of the selected files. The lines are available for the command in $_. If you want multiple lines, just finish all lines but the last with a \ backslash character, which will eventually not be the part of the command.

Typical usage:

Start directory? [.]./javahelp
File pattern:*.html
Recurse subdirectories? [n]y
Keep original file times? [n]y
Save undo information to file [none]?undo.txt
Command to execute for each line:
s/Java/Borneo/g
which will convert each html file in your Java tutorial, and will change all occurrences of Java to Borneo. Isn't it fine? Well, then better undo it, but instead just doing
Start directory? [.]./javahelp
File pattern:*.html
Recurse subdirectories? [n]y
Keep original file times? [n]y
Save undo information to file [none]?undo.txt
Command to execute for each line:
s/Borneo/Java/g
you should use the -u option for undoing and answer undo.txt when the program asks
Undo file?
This will restore the original content even if Borneo was mentioned in the original files, e.g. Borneo and Javaletta written by Shakes Perl 5.00110 is it the last release?

Note that there is some deficiency in the current version of prep.pl restoring the original file times. It actually does not, when undoing the changes.

When you type the command, do NOT press return at the end of the line. Think it over again, and do NOT repeat my mistakes. If you did, you better let it go and use the undo file when it has finished. Do not press Control-C. Do not hurry using this program at no time.

WARNING: I just do not know what this program can do and undo to binary files. I do not know either what it does if you break it while running. You better do not use it at all, unless you are desperate. I told you. You have listened, understood, so here it is: prep.pl.