home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The CDPD Public Domain Collection for CDTV 1
/
CDPD_Vol1.bin
/
pd
/
101-125
/
102
/
match_stuff
/
scripts.doc
< prev
next >
Wrap
Text File
|
1992-06-23
|
9KB
|
210 lines
Command Scripts for Pattern matching
____________________________________
Here are some instructions on how to use the command scripts included in
this package. Look at the scripts themselves to see the full gory details.
In the following I will assume you are using Sili(Con:) to execute the
scripts as commands. If you aren't (shame!), just preface all the command
lines shown by "EXECUTE" (or whatever you have renamed that command to).
PR -- Print files with matching names
This is a simple script that is in some ways more versatile than the
PRINTFILES program supplied with 1.2 in that you can use pattern
specifiers as well as plain file names. It runs in the bacground,
sending the specified files to the printer (PRT:) separated by
form-feeds. (A minor annoyance: the ECHO command used to generate the
form-feed also insists on a newline to follow; you might want to
consider Bryce Nesbitt's ECHO replacement if you have it around.)
For example to print all .DOC files in the current directory:
PR #?.doc
You can include up to five filename specifiers in the command line.
These can include directory paths which in turn can contain patterns.
To look through all the directories at the top level of df1: and print
any files with DOC or README in their name:
PR df1:#?/#?(doc|readme)#?
A word of caution: if you do use directory paths, it is best to specify
a complete path each time, from the device on down; if a previous
specifier references a different directory from the current one, the
later ones MUST all have complete paths. (This is because the script
has to change directories in such a case, and it will get lost if full
paths aren't given.)
Let's use this PR script to demonstrate some of the added features of
Mat pattern matching. (All these remarks and cautions about patterns
and file specifiers apply to the other scripts as well.) For a start,
if you prefer "*" to "#?", Mat allows that instead (provided it is not
inside quotes, where EXECUTE will misunderstand its meaning):
PR *.doc
If you want to print out all the files in the current directory that
are NOT .INFO files, use:
PR #?~(.info)
[If you've been reading ahead, don't try the "slicing" feature with PR!
Only the REN script in this set uses that.]
REF -- Search Text files for patterns
REF has somewhat the same function as the DOS command SEARCH, but the
differences are many. The first one you will notice is that REF is
SLOW! On the other hand it's doing a lot more, as we'll see, so be
patient. To ameliorate this it runs in the background, and prints out
everything at once when done. [The script as supplied writes
everything to a temporary file in ram, and deletes this when it has
been printed, but you could easily change this to make a more permanent
record.]
You can use any of Mat's pattern matching features to specify the
string to be searched for. And of course you can use patterns to
specify which files are to be searched (as in PR); up to three
specifiers are allowed. All lines found that include the pattern
anywhere within them are displayed, with the actual match section
highlighted. REF makes two assumptions in its matching: a) you want to
match the pattern anywhere it occurs on the line; b) you want any
caps/lower-case difference ignored. If these aren't valid assumptions,
use Mat directly (or rewrite REF). Don't forget to put quotes around
your pattern if it contains spaces.
One example out of the endless possibilities should suffice:
REF "(pattern|string) match" #?.doc
will find all the lines that contain "pattern match" or "string match"
in all the .DOC files in this directory.
I'll remind you that this script is rather simple minded. It doesn't
use any of the features in Mat for displaying file names, line numbers
and so on. Modify it to your needs.
REN -- Global rename with pattern matching.
This script I use a lot, but it needs more knowledge of Mat's pattern
matching features than the previous ones. The thing is that when you
have the freedom AmigaDOS gives you in naming files (compared to
certain operating systems on certain other machines that we will
ignore...) you need much more than the "ren *.c *.bak" convention
that suffices for those primitive systems; we have no "root names" and
"extensions" to be treated as indivisible units. To provide a general
renaming facility we have to introduce concepts like "slices" and
"templates".
As an example, suppose I have some text files that all end in '.txt',
and I want to rename these to, say, '--.txt_OLD'. It happens though
that they all have associated icons '--.txt.info' which will have to be
renamed too, and if they are to remain WorkBench-accessible the ".info"
string must stay at the end of the name. So, how do we do this with
REN? Like this:
REN #?.txt^#? AS ^0_OLD^1
Yipes! Whad'all'dat?
OK. So I jumped in at the deep end. Be brave. Let's look at the
pattern first: it's fairly standard except for the little "^" that
crept in after "txt"; this is a "slice mark" that indicates where we
want to split up the matched string. Each resulting match can be
referenced in two parts: 1) everything up to ".txt", and 2) everything
after that (this could be empty). By the way, that mark is NOT
produced with the CTRL key: all references to "^" here mean the actual
circumflex ("hat") character -- "SHIFT-6" on your keyboard.
Then there's that very strange second command argument (the keyword
"AS" is optional); those slice marks seem to crop up here again. Only
-- sorry -- this time they're not slice marks: this is a "template",
and they -- with the character immediately following -- are
"selectors". The template indicates exactly how the matched string is
to be rearranged (the script itself extends this considerably to do the
actual renaming job, but don't worry about that here). Breaking the
template up, the first two characters are "^0"; this means "use the
part of the match before the slice mark". Following this, "_OLD" is the
literal string we want inserted at that point. Finally, "^1" means "use
the rest of the match".
As a result of this command, then, we would see this sort of renaming:
myfile.txt becomes myfile.txt_OLD
myfile.txt.info " myfile.txt_OLD.info
any other.txt " any other.txt_OLD
any other.txt_oops " any other.txt_OLD_oops
That last renaming operation mightn't be part what we wanted, so in
that case we would extend the previous command:
REN #?.txt^(%|.info) ^0_OLD^1
which would ONLY rename files that ENDED either with '.txt' or with
'.txt.info'.
Another common sort of renaming is not to add, but to change, a segment
of the name:
myfile.txt --> myfile.OLD
myfile.txt.info --> myfile.OLD.info
...and so on
For this, we need TWO slice marks:
REN #?.^txt^#? ^0OLD^2
In this case "^0" refers to the match up to the ".". "^1" is the piece
between the first and second slice marks -- i.e. "txt". It doesn't
appear in the template this time -- it is discarded and replaced by
"OLD". "^2" is now the remainder of the match following the second
slice.
Of course the renaming job is often simpler than this. For example to
move all your '.c' files to an existing subdirectory 'old', just use:
REN #?.c old/^F
"^F" is the selector for the original file name, so this just prefixes
"old/" to it. And you can always reduce to the basics:
REN myfile yourfile
All simple now? ..It gets easy with a little practice. For the whole
story on slicing and templates, read Mat.DOC.
UPDATE -- Update files in a backup directory
This script calls the public domain copy program "xcopy" (which should
be available in your command directory path somewhere) to transfer
files to a backup directory only if they are more recent than those
already in the directory. In its simplest form it is just an
alternative way of invoking xcopy, but it also allows complete pattern
specification of the source files (xcopy uses the MS-DOS wild card
convention only).
UPDATE TO DF1:backup
will copy all files in the current directory to DF1:backup if they
are more recent than those already there (or are not there at all).
(The "TO" keyword is required in this form.)
UPDATE #?~(.bak) DF1:backup
will copy all the (more recent) files EXCEPT those ending in ".bak" to
the destination. (The "TO" keyword could be omitted in this case.)
This script only allows one file specifier (not an inherent limitation
-- just didn't seem one would often need more); of course it can be a
full path if you wish. It checks for the existence of the destination,
but beware: it can't tell whether it's a directory or not!
%%%%%%%%%%%%%%%%%