N U T S H E L L | |
Product | MultiFileReplace |
---|---|
Description | Multi-file text and pattern replacer |
Language | Perl |
Interface | command-line |
Platform | any Perl-supported (Windows,Linux,...) |
Reference | API |
What if you had a large collection of files, contained in multiple directories, and you wanted to, say, update your email address stored in the header of each file? You need a utility that will search for your email address, replace it with a new one, and do this for all files in the current directory as well as in each subdirectory from the current directory.
What if you wanted to insert a common block of text (i.e. multiple lines) just before the last line of your current file header (for instance, adding verbiage for licensing)? You need a utility that can search for patterns, not just text, and handle multiple-line blocks of text.
What if you wanted to use full pattern matching to locate target text and replace it with another string that should include the name of the file being processed? You need a utility that handles regular expressions plus provides predefined variables for file and path names that can be incorporated into a replacement string.
You are completely surprised to learn, I am sure, that MultiFileReplace does all that. Now, Unix/Linux users who understand the intricacies of the find command in conjunction with sed, awk, or perl, may not be swayed, but this little tool is quite useful for helping to make a collection of files more uniform in structure. I wrote it because I was getting ready to release the CleanCode files for consumption, and needed to update the license, tidy up the header, including adding references to the corresponding documentation.
While MultiFileReplace is fine for doing plain string replacements, and requires no prerequisite knowledge to use ordinary strings, its power comes into play when you do pattern replacements. Patterns, or more generally regular expressions, provide tremendous flexibility in specifying a target to match, but you have to understand how to use regular expressions first. I'll wait...
MultiFileReplace has a few features that even Unix and Linux users might appreciate:
This first example shows the more concise of the two preview modes, specifying the disposition of all files selected after filtering is applied.
replace.pl -l -e pm "xyz" "[PATHNAME]" . Files will *not* be updated. ./CGI/Handy.pm...will be changed ./CGI/PageGenerator.pm...will be changed ./CGI/PageSequencer.pm...will be changed ./CGI/PageValidator.pm...will be changed ./CGI/PageValidator/CheckItem.pm...will not change ./CGI/PageValidator/LibraryMgr.pm...will not change ./CGI/PageValidator/Plugin.pm...will be changed ./CGI/PageValidator/PluginLib/CCLib.pm...will be changed ./CGI/PageValidator/Plugin/CreditCardNum.pm...will be changed ./CGI/PageValidator/Plugin/Date.pm...will be changed ./CGI/PageValidator/Plugin/IsAmex.pm...will be changed ./CGI/PageValidator/Plugin/SamePassword.pm...will be changed ./CGI/PageValidator/MainLib/SysLib.pm...will be changed ./CGI/PageValidator/MainLib/UserLib.pm...will be changed ./CGI/PagePlugin/FormMail.pm...will be changed
This second example, on the other hand, shows the verbose preview mode (particularly so since multiple lines are involved). Here one may observe that the first two files had the target text, while the last three did not match, so would not be modified. Since the auto-block option was also specified, the first and last lines snap to their line boundaries as well. (If the target is confined to a single line, the output is more concise.)
replace.pl -Lb -e java "contents of this.*?copy of the License" "" . Files will *not* be updated. xml/DocumentPlus.java... ==== * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at xml/NodeExtractor.java... ==== * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at xml/SimpleValidate.java... xml/TransformTest.java... xml/ValidateTest.java...Go to Perl tech docs