![]() |
The Kermit Project |
Now hosted by
Panix.com
New York City USA •
kermit@kermitproject.org
| ||||||||
|
Replacetextblock | [download] |
Author: Frank da Cruz
Script version: 1.01
Dated: 2014/02/28Platform: Any version of Unix where C-Kermit is available.
Requires: C-Kermit 9.0 or later
This page last updated: Fri Feb 28 11:46:39 2014
Replacetextblock is a program written in the Kermit script language that answers the frequently asked question, “How do I replace a block of text in a group of files with a different block of text without having to manually edit each file?”. A Google search doesn't turn up any good answers, so here is a Kermit script that probably does just want you want.
cd (to desired directory that is in your PATH)If not, visit the getkermitscript page to download and install it, or at least to read how to download and install scripts the traditional tedious way (save, chmod, edit...). If you just want to look at the source code (and maybe save it), CLICK HERE.
getkermitscript replacetextblock
C-Kermit 9.0.304 already has a CHANGE command that can be used to replace text within a line or whole text lines in a group of files, but the CHANGE command does not operate on a blocks of text, such as:
In fact, Replacetextblock is even a bit more flexible that that. Suppose, for example, you have software source code broken up into 200 modules. Each module contains a copyright notice, but the year might different:
/*Other source files might say "Copyright (c) 1986, 2007", or other years. Suppose that for some reason (for example, the copyright was sold to another company) you want to change the text of all these notices, and also to update the copyright year. You can create an "oldtext" file containing a lines that contain patterns that match these notices:
* Copyright (c) 1986, 1990, XYZ Corporation. All rights reserved.
* See the COPYING file for copyright and license information.
*/
\* Copyright (c) 1986, {19,20}[0-9][0-9], XYZ Corporation. All rights reserved.
\* See the COPYING file for copyright and license information.
“{19,20}[0-9][0-9]” is a pattern that matches all four-digit numbers starting with 19 or 20, i.e. all years in the 20th and 21st Centuries. Backslash (\) has been placed in front of the asterisks because '*' is a pattern-matching character but in this case we want to take it literally. You can read about Kermit's pattern matching HERE.
Replacetextblock uses the oldtext file to search through the files you've specified. Then a second file, the newtext file, which contains the new replacement text is substituted into each file in place of the lines that match the first file. The newtext file might look like this:
* Copyright (c) 1986, 2014, ABC Corporation. All rights reserved.Then to update all the files (let's suppose they are C-language source files), you would do something like this:
* This package has new licensing terms.
* See the LICENSE file for copyright and license information.
replacetextblock oldtext newtext *.[ch] makefile
If you want to remove the oldtext without replacing it with anything, you can give the name of an empty file or simply use Unix's built-in empty file, /dev/null for newtext:
replacetextblock oldtext /dev/null *.htmlNormally, as the script runs, it prints the name of each file it is processing, and whether it it found and replaced the desired text:
[~/mm] replacetextblock oldtext newtext [ab]*.[ch]If the text was not found in a particular file, that file is not touched. If a file was changed, the script makes a backup of the original file having the same name but with .backup appended to it (you can defeat this behavior with a command-line option, next section). The updated file has the same permissions as the original, but its modification time is updated.
action.c...OK (text was replaced)
alias.c...OK (text was replaced)
args.h...(replacement text not found - no change was made)
argsort.c...OK (text was replaced)
babyl.h...OK (text was replaced)
The size and number of the lines in the oldtext and newtext files don't have to be the same; you can (for example) replace a short text with a long one or vice versa.
replacetextblock [ option [ option [ ... ] ] ] oldtext newtext file [ file [ ... ] ] ]That is, zero or more optional parameters followed by:
Be very careful not to omit the oldtext or newtext arguments. If you start the script with something like "replacetext *.txt" it will treate the first file that matches the wildcard as the oldtext file and the second as the newtext file, which is probably not what you intended. There's no way the script could prevent this. (Well, I suppose it would be possible to have -oldtext:xxx and -newtext:yyy options but let's leave that for version 2).
Options start with a hyphen ('-'). The options are:
replacetextblock -except:\*-old.\* oldtext newtext *.[ch]
Filenames should not start with hyphen; otherwise the script will think they are options. If you need to process a group of files where one or more of the names might start with a hyphen, use this notation:
replacetextblock -except:\*-old.\* oldtext newtext ./*.[ch]that is, prefix the filename or wildcard with “./”.