home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d102 / match_stuff.lha / Match_Stuff / Scripts.DOC < prev    next >
Text File  |  1987-09-06  |  9KB  |  210 lines

  1.                   Command Scripts for Pattern matching
  2.                   ____________________________________
  3.  
  4. Here are some instructions on how to use the command scripts included in
  5. this package.  Look at the scripts themselves to see the full gory details.
  6.  
  7. In the following I will assume you are using Sili(Con:) to execute the
  8. scripts as commands.  If you aren't (shame!), just preface all the command
  9. lines shown by "EXECUTE" (or whatever you have renamed that command to).
  10.  
  11.  
  12. PR      --  Print files with matching names
  13.  
  14.     This is a simple script that is in some ways more versatile than the
  15.     PRINTFILES program supplied with 1.2 in that you can use pattern
  16.     specifiers as well as plain file names.  It runs in the bacground,
  17.     sending the specified files to the printer (PRT:) separated by
  18.     form-feeds. (A minor annoyance: the ECHO command used to generate the
  19.     form-feed also insists on a newline to follow; you might want to
  20.     consider Bryce Nesbitt's ECHO replacement if you have it around.)
  21.  
  22.     For example to print all .DOC files in the current directory:
  23.  
  24.                 PR #?.doc
  25.  
  26.     You can include up to five filename specifiers in the command line.
  27.     These can include directory paths which in turn can contain patterns.
  28.     To look through all the directories at the top level of df1: and print
  29.     any files with DOC or README in their name:
  30.  
  31.                 PR df1:#?/#?(doc|readme)#?
  32.  
  33.     A word of caution: if you do use directory paths, it is best to specify
  34.     a complete path each time, from the device on down; if a previous
  35.     specifier references a different directory from the current one, the
  36.     later ones MUST all have complete paths. (This is because the script
  37.     has to change directories in such a case, and it will get lost if full
  38.     paths aren't given.)
  39.  
  40.     Let's use this PR script to demonstrate some of the added features of
  41.     Mat pattern matching.  (All these remarks and cautions about patterns
  42.     and file specifiers apply to the other scripts as well.)  For a start,
  43.     if you prefer "*" to "#?", Mat allows that instead (provided it is not
  44.     inside quotes, where EXECUTE will misunderstand its meaning):
  45.  
  46.                 PR *.doc
  47.  
  48.     If you want to print out all the files in the current directory that
  49.     are NOT .INFO files, use:
  50.  
  51.                 PR #?~(.info)
  52.  
  53.     [If you've been reading ahead, don't try the "slicing" feature with PR!
  54.     Only the REN script in this set uses that.]
  55.  
  56.  
  57. REF     --  Search Text files for patterns
  58.  
  59.     REF has somewhat the same function as the DOS command SEARCH, but the
  60.     differences are many.  The first one you will notice is that REF is
  61.     SLOW!  On the other hand it's doing a lot more, as we'll see, so be
  62.     patient.  To ameliorate this it runs in the background, and prints out
  63.     everything at once when done.  [The script as supplied writes
  64.     everything to a temporary file in ram, and deletes this when it has
  65.     been printed, but you could easily change this to make a more permanent
  66.     record.]
  67.  
  68.     You can use any of Mat's pattern matching features to specify the
  69.     string to be searched for.  And of course you can use patterns to
  70.     specify which files are to be searched (as in PR); up to three
  71.     specifiers are allowed.  All lines found that include the pattern
  72.     anywhere within them are displayed, with the actual match section
  73.     highlighted.  REF makes two assumptions in its matching: a) you want to
  74.     match the pattern anywhere it occurs on the line; b) you want any
  75.     caps/lower-case difference ignored.  If these aren't valid assumptions,
  76.     use Mat directly (or rewrite REF).  Don't forget to put quotes around
  77.     your pattern if it contains spaces.
  78.  
  79.     One example out of the endless possibilities should suffice:
  80.  
  81.             REF "(pattern|string) match" #?.doc
  82.  
  83.     will find all the lines that contain "pattern match" or "string match"
  84.     in all the .DOC files in this directory.
  85.  
  86.     I'll remind you that this script is rather simple minded. It doesn't
  87.     use any of the features in Mat for displaying file names, line numbers
  88.     and so on.  Modify it to your needs.
  89.  
  90.  
  91. REN     --  Global rename with pattern matching.
  92.  
  93.     This script I use a lot, but it needs more knowledge of Mat's pattern
  94.     matching features than the previous ones.  The thing is that when you
  95.     have the freedom AmigaDOS gives you in naming files (compared to
  96.     certain operating systems on certain other machines that we will
  97.     ignore...) you need much more than the "ren *.c *.bak" convention
  98.     that suffices for those primitive systems; we have no "root names" and
  99.     "extensions" to be treated as indivisible units.  To provide a general
  100.     renaming facility we have to introduce concepts like "slices" and
  101.     "templates".
  102.  
  103.     As an example, suppose I have some text files that all end in '.txt',
  104.     and I want to rename these to, say, '--.txt_OLD'.  It happens though
  105.     that they all have associated icons '--.txt.info' which will have to be
  106.     renamed too, and if they are to remain WorkBench-accessible the ".info"
  107.     string must stay at the end of the name.  So, how do we do this with
  108.     REN?  Like this:
  109.  
  110.             REN #?.txt^#? AS ^0_OLD^1
  111.  
  112.     Yipes!  Whad'all'dat?
  113.  
  114.     OK.  So I jumped in at the deep end.  Be brave.  Let's look at the
  115.     pattern first: it's fairly standard except for the little "^" that
  116.     crept in after "txt"; this is a "slice mark" that indicates where we
  117.     want to split up the matched string.  Each resulting match can be
  118.     referenced in two parts: 1) everything up to ".txt", and 2) everything
  119.     after that (this could be empty).  By the way, that mark is NOT
  120.     produced with the CTRL key: all references to "^" here mean the actual
  121.     circumflex ("hat") character -- "SHIFT-6" on your keyboard.
  122.  
  123.     Then there's that very strange second command argument (the keyword
  124.     "AS" is optional); those slice marks seem to crop up here again.  Only
  125.     -- sorry -- this time they're not slice marks: this is a "template",
  126.     and they -- with the character immediately following -- are
  127.     "selectors".  The template indicates exactly how the matched string is
  128.     to be rearranged (the script itself extends this considerably to do the
  129.     actual renaming job, but don't worry about that here).  Breaking the
  130.     template up, the first two characters are "^0"; this means "use the
  131.     part of the match before the slice mark". Following this, "_OLD" is the
  132.     literal string we want inserted at that point. Finally, "^1" means "use
  133.     the rest of the match".
  134.  
  135.     As a result of this command, then, we would see this sort of renaming:
  136.  
  137.         myfile.txt          becomes     myfile.txt_OLD
  138.         myfile.txt.info        "        myfile.txt_OLD.info
  139.         any other.txt          "        any other.txt_OLD
  140.         any other.txt_oops     "        any other.txt_OLD_oops
  141.  
  142.     That last renaming operation mightn't be part what we wanted, so in
  143.     that case we would extend the previous command:
  144.  
  145.             REN #?.txt^(%|.info) ^0_OLD^1
  146.  
  147.     which would ONLY rename files that ENDED either with '.txt' or with
  148.     '.txt.info'.
  149.  
  150.     Another common sort of renaming is not to add, but to change, a segment
  151.     of the name:
  152.  
  153.         myfile.txt          -->     myfile.OLD
  154.         myfile.txt.info     -->     myfile.OLD.info
  155.         ...and so on
  156.  
  157.     For this, we need TWO slice marks:
  158.  
  159.             REN #?.^txt^#?  ^0OLD^2
  160.  
  161.     In this case "^0" refers to the match up to the ".".  "^1" is the piece
  162.     between the first and second slice marks -- i.e. "txt".  It doesn't
  163.     appear in the template this time -- it is discarded and replaced by
  164.     "OLD".  "^2" is now the remainder of the match following the second
  165.     slice.
  166.  
  167.     Of course the renaming job is often simpler than this.  For example to
  168.     move all your '.c' files to an existing subdirectory 'old', just use:
  169.  
  170.             REN #?.c old/^F
  171.  
  172.     "^F" is the selector for the original file name, so this just prefixes
  173.     "old/" to it.  And you can always reduce to the basics:
  174.  
  175.             REN myfile yourfile
  176.  
  177.     All simple now?  ..It gets easy with a little practice.  For the whole
  178.     story on slicing and templates, read Mat.DOC.
  179.  
  180.  
  181.  
  182. UPDATE  --  Update files in a backup directory
  183.  
  184.     This script calls the public domain copy program "xcopy" (which should
  185.     be available in your command directory path somewhere) to transfer
  186.     files to a backup directory only if they are more recent than those
  187.     already in the directory.  In its simplest form it is just an
  188.     alternative way of invoking xcopy, but it also allows complete pattern
  189.     specification of the source files (xcopy uses the MS-DOS wild card
  190.     convention only).
  191.  
  192.             UPDATE TO DF1:backup
  193.  
  194.     will copy all files in the current directory to DF1:backup if they
  195.     are more recent than those already there (or are not there at all).
  196.     (The "TO" keyword is required in this form.)
  197.  
  198.             UPDATE #?~(.bak) DF1:backup
  199.  
  200.     will copy all the (more recent) files EXCEPT those ending in ".bak" to
  201.     the destination. (The "TO" keyword could be omitted in this case.)
  202.  
  203.     This script only allows one file specifier (not an inherent limitation
  204.     -- just didn't seem one would often need more); of course it can be a
  205.     full path if you wish.  It checks for the existence of the destination,
  206.     but beware: it can't tell whether it's a directory or not!
  207.  
  208.                             %%%%%%%%%%%%%%%%%
  209.  
  210.