home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l224 / 1.img / MANUAL.ZIP / GREP.DOC < prev    next >
Encoding:
Text File  |  1990-10-29  |  13.3 KB  |  344 lines

  1.                   GREP: A FILE SEARCH UTILITY
  2.                   ---------------------------
  3.  
  4.  
  5. TABLE OF CONTENTS
  6. -----------------
  7. 1. What is GREP?
  8. 2. GREP Options
  9. 3. The Search String
  10. 4. Operators in Regular Expressions
  11. 5. The File Specification
  12. 6. GREP Examples
  13.  
  14. 1. WHAT IS GREP?
  15. ----------------
  16.  
  17.   GREP is a powerful search utility that can search for text in several files 
  18.   at once.
  19.  
  20.   The general command-line syntax for GREP follows:
  21.  
  22.     grep [options] searchstring filespec [filespec filespec ...filespec]
  23.  
  24.   For example, if you want to see in which source files you call the 
  25.   setupmodem function, you could use GREP to search the contents of all the 
  26.   .ASM files in your directory to look for the string setupmodem, like this:
  27.  
  28.     grep setupmodem *.asm
  29.  
  30.  
  31. 2. GREP OPTIONS
  32. ---------------
  33.  
  34.   In the command line, options are one or more single characters preceded by 
  35.   a hyphen symbol (-). Each individual character is a switch that you can turn
  36.   on or off: type the plus (+) after a character to turn the option on, or 
  37.   type a hyphen (-) after the character to turn the option off.
  38.  
  39.   The default is on (the + is implied): for example, -r means the same thing 
  40.   as -r+. You can list multiple options individually (like this: -i -d -l), 
  41.   or you can combine them (like this: -ild or -il -d, and so forth); they're 
  42.   all the same to GREP.
  43.  
  44.   Here is a list of the option characters used with GREP and their meanings:
  45.  
  46.   -c  Count only: Only a count of matching lines is printed. For each file 
  47.       that contains at least one matching line, GREP prints the file name and
  48.       a count of the number of matching lines. Matching lines are not printed.
  49.  
  50.   -d  Directories: For each filespec specified on the command line, GREP 
  51.       searches for all files that match the file specification, both in the 
  52.       directory specified and in all subdirectories below the specified 
  53.       directory. If you give a filespec without a path, GREP assumes the 
  54.       files are in the current directory.
  55.  
  56.   -i  Ignore case: GREP ignores uppercase/lowercase differences (case-folding).
  57.       GREP treats all letters a-z as being identical to the corresponding 
  58.       letters A-Z in all situations.
  59.  
  60.   -l  List match files: Only the name of each file containing a match is 
  61.       printed. After GREP finds a match, it prints the file name and 
  62.       processing immediately moves on to the next file.
  63.  
  64.   -n  Numbers: Each matching line that GREP prints is preceded by its 
  65.       line number.
  66.  
  67.   -o  UNIX output format: Changes the output format of matching lines to 
  68.       support more easily the UNIX style of command-line piping. All lines 
  69.       of output are preceded by the name of the file that contained the 
  70.       matching line.
  71.  
  72.   -r  Regular expression search: The text defined by searchstring is treated 
  73.       as a regular expression instead of as a literal string.
  74.  
  75.   -u  Update options: GREP will combine the options given on the command line 
  76.       with its default options and write these to the GREP.COM file as the 
  77.       new defaults. (In other words, GREP is self-configuring.) This option 
  78.       allows you to tailor the default option settings to your own taste.
  79.  
  80.   -v  Non-match: Only non-matching lines are printed. Only lines that do not 
  81.       contain the search string are considered to be non-matching lines.
  82.  
  83.   -w  Word search: Text found that matches the regular expression will be 
  84.       considered a match only if the character immediately preceding and 
  85.       following cannot be part of a word. The default word character set 
  86.       includes A-Z, 9-0, and the underscore (_). An alternate form of this 
  87.       option allows you to specify the set of legal word characters. Its form
  88.       is -w[set], where set is any valid regular expression set definition. 
  89.       If alphabetic characters are used to define the set, the set will 
  90.       automatically be defined to contain both the uppercase and lowercase 
  91.       values for each letter in the set, regardless of how it is typed, even 
  92.       if the search is case-sensitive. If the -w option is used in combination
  93.       with the -u option, the new set of legal characters is saved as the 
  94.       default set.
  95.  
  96.   -z  Verbose: GREP prints the file name of every file searched. Each matching 
  97.       line is preceded by its line number. A count of matching lines in each 
  98.       file is given, even if the count is zero.
  99.  
  100.   Remember that each of GREP's options is a switch: its state reflects the way 
  101.   you last "flipped" it. At any given time, each option can only be on or off. 
  102.   Each occurrence of a given option on the command line overrides its previous
  103.   definition. For example,
  104.  
  105.     grep -r -i- -d -i -r-  main( my*.asm
  106.  
  107.   Given this command line, GREP will run with the -d option on, the -i option 
  108.   on, and the -r option off.
  109.  
  110.   You can install your preferred default setting for each option in GREP.COM 
  111.   with the -u option. For example, if you want GREP to always do a verbose 
  112.   search (-z on), you can install it with the following command:
  113.  
  114.     grep -u -z
  115.  
  116.  
  117. 3. THE SEARCH STRING
  118. --------------------
  119.  
  120.   The value of the search string defines the pattern GREP will search for. 
  121.   A search string can be either a regular expression or a literal string. In 
  122.   a regular expression, certain characters have special meanings: they are 
  123.   operators that govern the search. In a literal string, there are no 
  124.   operators; each character is treated literally.
  125.  
  126.   You can enclose the search string in quotation marks to prevent spaces and 
  127.   tabs from being treated as delimiters. Matches will not cross line boundaries
  128.   (a match must be contained in a single line).
  129.  
  130.   An expression is either a single character or a set of characters enclosed 
  131.   in brackets. A concatenation of regular expressions is a regular expression.
  132.  
  133.  
  134. 4. OPERATORS IN REGULAR EXPRESSIONS
  135. -----------------------------------
  136.  
  137.   When you use the -r option, the search string is treated as a regular 
  138.   expression (not a literal expression) and the following characters take on 
  139.   special meanings:
  140.  
  141.   ^  A circumflex at the start of the expression matches the start of a line.
  142.  
  143.   $  A dollar sign at the end of the expression matches the end of a line.
  144.  
  145.   .  A period matches any character.
  146.  
  147.   *  An expression followed by an asterisk wildcard matches zero or more 
  148.      occurrences of that expression. For example, in fo*, the * operates on 
  149.      the expression o; it matches f, fo, foo, and so on. (f followed by zero 
  150.      or more os), but doesn't match fa.
  151.  
  152.   +  An expression followed by a plus sign matches one or more occurrences 
  153.      of that expression: fo+ matches fo, foo, and so on, but not f.
  154.  
  155.   [] A string enclosed in brackets matches any character in that string, 
  156.      but no others. If the first character in the string is a circumflex (^), 
  157.      the expression matches any character except the characters in the string. 
  158.      For example, [xyz] matches x, y, or z, while [^xyz} matches a and b, but 
  159.      not x, y, or z. You can specify a range of characters with two characters
  160.      separated by a hyphen (-). These can be combined to form expressions (like 
  161.      [a-bd-z?] to match ? and any lowercase letter except c).
  162.  
  163.   \  The backslash escape character tells GREP to seach for the literal 
  164.      character that follows it. For example, \. matches a period instead of 
  165.     "any character."
  166.  
  167.   Note: Four of the previously described characters ($, ., *, and +) do not 
  168.   have any special meaning when used within a bracketed set. In addition, 
  169.   the character ^ is only treated specially if it immediately follows the 
  170.   beginning of the set definition (that is, immediately after the [).
  171.  
  172.   Any ordinary character not mentioned in the preceding list matches that 
  173.   character. (> matches >, # matches #, and so on.)
  174.  
  175.  
  176. 5. THE FILE SPECIFICATION
  177. -------------------------
  178.  
  179.   The third item in the GREP command line is filespec, the file specification; 
  180.   it tells GREP which files (or groups of files) to search. filespec can be an 
  181.   explicit file name, or a generic file name incorporating the DOS ? and * 
  182.   wildcards. In addition, you can enter a path (drive and directory 
  183.   information) as part of filespec. If you give filespec without a path, 
  184.   GREP only searches the current directory.
  185.  
  186.  
  187. 6. GREP EXAMPLES
  188. ----------------
  189.  
  190.   The following examples assume that all of GREP's options default to off:
  191.  
  192.   Example 1
  193.   ---------
  194.   Command line:    grep start: *.asm
  195.  
  196.   Matches:         start:
  197.                    restart:
  198.  
  199.   Does not match:  restarted:
  200.                    ClockStart:
  201.  
  202.   Files Searched:  *.ASM in current directory.
  203.  
  204.   Note: By default, the search is case-sensitive.
  205.  
  206.  
  207.   Example 2
  208.   ---------
  209.   Command line:    grep -r [^a-z]main\ *( *.asm
  210.  
  211.   Matches:         main(i:integer)
  212.                    main(i,j:integer)
  213.                    if (main  ()) halt;
  214.  
  215.   Does not match:  mymain()
  216.                    MAIN(i:integer);
  217.  
  218.   Files Searched:  *.ASM in current directory.
  219.  
  220.   Note: GREP searches for the word main with no preceding lowercase letters 
  221.   ([^a-z]), followed by zero or more occurrences of blank spaces (\ *), then 
  222.   a left parenthesis.
  223.  
  224.   Since spaces and tabs are normally considered to be command line delimiters, 
  225.   you must quote them if you want to include them as part of a regular 
  226.   expression. In this case, the space after main is quoted with the backslash 
  227.   escape character. You could also accomplish this by placing the space in 
  228.   double quotes
  229.  
  230.     [^a-z]main" "*
  231.  
  232.  
  233.   Example 3
  234.   ---------
  235.   Command line:    grep -ri [a-c]:\\data\.fil *.asm *.inc
  236.  
  237.   Matches:         A:\data.fil
  238.                    c:\Data.Fil
  239.                    B:\DATA.FIL
  240.  
  241.   Does not match:  d:\data.fil
  242.                    a:data.fil
  243.  
  244.   Files Searched:  *.ASM and *.INC in current directory.
  245.  
  246.   Note: Because the backslash and period characters (\ and .) usually have 
  247.   special meaning, if you want to search for them, you must quote them by 
  248.   placing the backslash escape character immediately in front of them.
  249.  
  250.  
  251.   Example 4
  252.   ---------
  253.   Command line:    grep -ri [^a-z]word[^a-z] *.doc
  254.  
  255.   Matches:         every new word must be on a new line.
  256.                    MY WORD!
  257.                    word--smallest unit of speech.
  258.                    In the beginning there was the WORD, and the WORD
  259.  
  260.   Does not match:  Each file has at least 2000 words.
  261.                    He misspells toward as toword.
  262.  
  263.   Files Searched:  *.DOC in the current directory.
  264.  
  265.   Note: This format basically defines how to search for a given word.
  266.  
  267.  
  268.   Example 5
  269.   ---------
  270.   Command line:    grep -iw word *.doc
  271.  
  272.   Matches:         every new word must be on a new line However,
  273.                    MY WORD!
  274.                    word: smallest unit of speech that conveys meaning.
  275.                    In the beginning there was the WORD, and the WORD
  276.  
  277.   Does not match:  each document contains at least 2000 words!
  278.                    He seems to continually misspell "toward" as "toword."
  279.  
  280.   Files searched:  *.doc in the current directory.
  281.  
  282.   Note: This format defines a basic "word" search.
  283.  
  284.  
  285.   Example 6
  286.   ---------
  287.   Command line:    grep "search string with spaces" *.doc *.asm
  288.                    a:\work\myfile.*
  289.  
  290.   Matches:         This is a search string with spaces in it.
  291.  
  292.   Does not match:  THIS IS A SEARCH STRING WITH SPACES IN IT.
  293.                    This is a search string with many spaces in it.
  294.  
  295.   Files Searched:  *.DOC and *.ASM in the current directory, and MYFILE.*
  296.                      in a directory called \WORK on drive A:.
  297.  
  298.   Note: This is an example of how to search for a string with embedded spaces.
  299.  
  300.  
  301.   Example 7
  302.   ---------
  303.   Command line:    grep -rd "[ ,.:?'\"]"$ \*.doc
  304.  
  305.   Matches:         He said hi to me.
  306.                    Where are you going?
  307.                    Happening in anticipation of a unique situation,
  308.                    Examples include the following:
  309.                    "Many men smoke, but fu man chu."
  310.  
  311.   Does not match:  He said "Hi" to me
  312.                    Where are you going? I'm headed to the beach this
  313.  
  314.   Files Searched:  *.DOC in the root directory and all its subdirectories
  315.                    on the current drive.
  316.  
  317.   Note: This example searches for the characters ,.:?' and " at the end of a 
  318.   line. Notice that the double quote within the range is preceded by an 
  319.   escape character so it is treated as a normal character instead of as the 
  320.   ending quote for the string. Also, notice how the $ character appears 
  321.   outside of the quoted string. This demonstrates how regular expressions can 
  322.   be concatenated to form a longer expression.
  323.  
  324.  
  325.   Example 8
  326.   ---------
  327.   Command line:    grep -ild " the " \*.doc
  328.                    or grep -i -l -d " the " \*.doc
  329.                    or grep -il -d " the " \*.doc
  330.  
  331.   Matches:         Anyway, this is the time we have
  332.                    do you think? The main reason we are
  333.  
  334.   Does not match:  He said "Hi" to me just when I
  335.                    Where are you going? I'll bet you're headed to
  336.  
  337.   Files Searched:  *.DOC in the root directory and all its subdirectories
  338.                    on the current drive.
  339.  
  340.   Note: This example ignores case and just prints the names of any files that 
  341.   contain at least one match. The three examples show different ways of 
  342.   specifying multiple options.
  343.  
  344.