![]() |
The Kermit Project
|
Now hosted by Panix.com
New York City USA
•
kermit@kermitproject.org
…since
1981
|
Espaņol | C-Kermit | E-Kermit | Kermit 95 | Scripts | News | About | FAQ |
Frank da Cruz
January 30, 2016
Last update: Mon Feb 1 19:54:04 2016
The CHANGE command, new to C-Kermit 9.0.304, lets you replace strings in one or more text files with new strings in a way that is more convenient and less error-prone than changing each file by hand, and is more portable and flexible than using a Unix sed script or other platform-specific method.
Suppose (for example) you manage a website with thousands of files in a directory tree, many of which link to an offsite page whose URL has just changed. Now you can fix them all with a single C-Kermit command:
change /recursive ~/web/*.html www.oldname.com www.newname.com
The syntax of the CHANGE command is:
CHANGE [ switches ] filespec String1 [ String2 ]The CHANGE command replaces all occurrences of string1 with string2 in the specified file or files. It works line by line; it does not do multiline or across-line substitutions (you can use the replacetextblock script for that). To remove strings from files, specify string2 as "" or omit string2. The filespec and strings can be given literally or you can use variables for them.
Square brackets indicate switches (explained below) that are optional. The filespec is a "file specification", which can be the name of a single file, possibly with a relative or absolute path included, or a "wildcard" pattern that uses special characters to match filenames, such as "*.html" to match all HTML files (see below for details about wildcards).
String1 and String2 should be enclosed in doublequotes "" or braces {} if if they contain spaces; for example, to update the copyright notice in a collection of C-language source files:
change *.[ch] "Copyright (C) 1985, 2015" "Copyright (C) 1985, 2016"In the event that String1 or String2 already contain braces or doublequotes, especially if these are not balanced, some quoting may be required. Or you can assign the strings to variables and then use the variable names in the CHANGE command; example:
.a = {value="./ .b = {value="../ change *.html \m(a) \m(b)Since the CHANGE command works line by line, only text files can be changed; C-Kermit automatically skips over binary files.
Normally the result file is a temporary file, which is then renamed back to the original file's name, thus overwriting it. Other options explained below allow you to back up the original file or to write the result file into a different directory. Each of these options preserves a copy of the original file.
Temporary files are created in the directory indicated by Kermit's \v(tmpdir) variable (“show var tmpdir”). If the temporary directory does not exist, or none is specified, you'll get an error message. You can specify a temporary directory with the SET TEMP-DIRECTORY command, or by setting a environment variable (Unix) or symbol or logical name (VMS) such as TMP, TEMP, or TEMPDIR to the full path of the desired directory, to which you must have write access. If you haven't defined a temporary directory or an environment variable for this, C-Kermit uses the underlying operating system's convention, such as the /tmp directory in Unix.
All temporary files are deleted after use.
/after: /destination: /modtime: /recursive /backup: /dotfiles /nodotfiles /simulate /before: /except: /norecursive /smaller-than: /case: /larger-than: /not-after: /count: /list /not-before:...for file selection (size, date, exception lists, dot-files), file disposition, directory traversal (flat or recursive), modification time of changed files (updated or preserved), and reporting of results.
/after:21-jan-2016_12:00:00 | Contains no spaces |
/after:"21 jan 2016 12:00:00" | Contains spaces, use quotes |
/after:{jan 21, 2016 12:00} | Contains spaces, use braces |
/after:"jan 21, 2016" | No time is given, 0:00:00 is assumed |
/after:1/21/2016 | No time is given, 0:00:00 is assumed |
/after:21/1/2016 | 21 is always a day, not a month |
/after:YESTERDAY | Symbolic date |
/after:8:00 | No date given; current date assumed |
/after:8:00pm | AM or PM can be specified |
/after:20:00 | Military time format can be used |
/after:"TODAY +7days" | offsets can be specified |
etc etc. For complete details about C-Kermit's date/time formats, CLICK HERE.
Switches must be separated from other command fields and each other by blank space. There must not be any blank space between a switch and its argument. If a switch argument contains any spaces, it must be enclosed in doublequotes or braces. Switches can be combined in any desired manner, for example:
change /before:1-feb-2016 /not-before:1-jan-2016 *.txt December Januarychanges all occurrences of the word “December” to “January” in all files having modification timestamps in January of 2016.
If you include the same switch more than once in a single CHANGE command, the value of the last instance is used.
Lots of C-Kermit's other file transfer and management have the same or similar switches: SEND, GET, DIRECTORY, DELETE, COPY, RENAME, TOUCH, and so on.
A “wildcard” is a notation used in a filename to match multiple files. For example, in “send *.txt” the asterisk is a wildcard, the meaning in this case is “send all files in the current directory whose name ends with .txt”. Most Kermit commands that accept filenames also accepts wildcards, the exception being commands that are allowed to operate on only one file, such as TRANSMIT. C-Kermit accepts the following wildcards:
* | (asterisk or "star") Matches any sequence of zero or more characters. For example, "ck*.c" matches all files whose names start with "ck" and end with ".c" including "ck.c". |
? | (question mark) Matches any single character. For example, "ck?.c" matches all files whose names are exactly 5 characters long and start with "ck" and end with ".c". When typing commands at the prompt, you must precede any question mark to be used for matching by a backslash (\) to override the normal function of question mark in interactive commands, which is to provide menus and file lists. You don't, however, need to quote filename question marks in command files (script programs). |
[abc] | Square brackets enclosing a list of characters matches any character in the list. Example: ckuusr.[ch] matches ckuusr.c and ckuusr.h. |
[a-z] | Square brackets enclosing a range of characters matches any character in the range; a hyphen (-) separates the low and high elements of the range. For example, [a-z] matches any letter from a to z. |
[acdm-z] | Lists and ranges may be combined. This example matches a, c, d, or any letter from m through z. |
{string1,string2,...} | Braces enclose a list of strings to be matched. For example: ck{ufio,vcon,cmai}.c matches ckufio.c, ckvcon.c, or ckcmai.c. The strings may themselves contain *, ?, [abc], [a-z], or other lists of strings. |
To force a special pattern character to be taken literally, precede it with a backslash, e.g. [a\-z] matches a, hyphen, or z rather than a through z. Or tell Kermit to SET WILDCARD-EXPANSION OFF before entering or referring to the filename (but then set it back ON afterwards to restore the normal behavior).
Similar notation can be used in general-purpose string matching; for example in the IF MATCH command:
if match ckufio.c {{ck[cuw]*.[cwh],makefile}} { echo "It's a C-Kermit source file" }Type HELP PATTERNS at the C-Kermit prompt for details. Also see HELP SET MATCH.