home *** CD-ROM | disk | FTP | other *** search
/ Big Blue Disk 20 / bbd20.zip / GREP.TXT < prev    next >
Text File  |  1988-03-29  |  7KB  |  131 lines

  1. |D╔══════════════════╗════════════════════════════════════════════════════════════
  2. |D║ |5The Happy Hacker |D║════════════════════════════════════════════════════════════
  3. |D╚══════════════════╝════════════════════════════════════════════════════════════
  4.  
  5. ^C^1GREP
  6. ^Cby
  7. ^CDaniel Tobias
  8.  
  9.    With a name like "Grep", it has to be good.  For years, this rather peculiar
  10. name has denoted a very useful command to the UNIX community.  GREP lets you
  11. search through a file or many files, looking for a particular pattern.  Let's
  12. say you vaguely remember that one of the text files on a disk had a reference
  13. to ducks that you'd like to find.  So, type "grep duck *.txt", and a list of
  14. all the lines with that word in any file with a .TXT extension will pop up.
  15.  
  16.    Even more complex things can be done, such as searching for patterns of
  17. characters more complex than that.  The syntax is as follows:
  18.  
  19.    ^1grep [options] expression [files]
  20.  
  21. where options (if present) is a hyphen (-) followed by a list of letters
  22. controlling some parameters of GREP, expression is the target pattern to look
  23. for, and files is a list of files (possibly including drive, path, and
  24. wildcards) separated by spaces, through which to search.  If the files are
  25. omitted, standard input is used; you would then have to type in the lines to be
  26. searched from the keyboard instead of getting them from a file, ending with
  27. CTRL-Z and ENTER.  This is not particularly useful by itself, but might be handy
  28. in a more complex expression using DOS I/O redirection or piping.  (If you don't
  29. know what these things are, ignore this and always use a filename when typing a
  30. GREP expression.)
  31.  
  32.    The normal result of GREP is to output all lines in the given file(s) which
  33. contain the given expression.  If multiple files are selected, the name of the
  34. file is shown with each line.  However, the effects of GREP can be modified by
  35. the options below.
  36.  
  37.    The option letters you can use are:
  38.  
  39.    v      Output all lines EXCEPT those which match.
  40.    c      Output only a count of matching lines, not the lines themselves.
  41.    i      Ignore upper/lowercase distinction when matching.
  42.    l      Output only the names of the files in which matches are found.
  43.    n      Precede each line by its relative line number in the file.
  44.    s      Suppress the error messages given for nonexistent files.
  45.    d      Recursively search subdirectories below current one as well.
  46.  
  47.    Hence, ^1grep -vi test junk.txt^0 outputs only those lines in JUNK.TXT that do
  48. not contain the string "test", ignoring case distinction: "TEST" is equivalent.
  49.  
  50.    The search expression syntax is as follows:
  51.  
  52.    1. An ordinary character (other than a period, asterisk, left square bracket,
  53. backslash, caret, dollar sign, or curly brace) matches itself.  Hence, the 
  54. letter "t" in a search expression tells that you are looking for the letter "t". 
  55.  
  56.    2. A backslash followed by one of the above-mentioned characters matches the
  57. following character.  This is the only way to do a search for one of these
  58. reserved characters.  For example, "\\" matches a backslash.
  59.  
  60.    3. A period (.) matches any character.
  61.  
  62.    4. A (non-empty) string of characters within square brackets matches any of
  63. the characters within the brackets.  Ranges can be indicated using the hyphen.
  64. Hence, "[abc]" will match the letter a, b, or c; [a-jx] will match any letter
  65. from a to j, or x.  Order of characters follows standard ASCII.
  66.    If the first character within the brackets is a caret (^^), the meaning is
  67. reversed: it matches anything EXCEPT the characters within the brackets.
  68.  
  69.    5. Any of the preceding followed by an asterisk (*) matches zero or more
  70. occurrences of the preceding item.  Hence, "b*" matches "" (null string), "b",
  71. "bb", "bbb", etc.  "[a-c]*" matches any number of occurrences of the letters a
  72. through c, with no other characters intervening.
  73.  
  74.    6. Any of the first four items followed by {m,n} (where m and n are integers 
  75. less than 256) matches between m and n consecutive occurrences of the preceding 
  76. item.  Hence, "[a-j]{2,12}" matches 2 through 12 occurrences of letters in the 
  77. range of a through j.  A single number like {m} means to match exactly m 
  78. occurrences; a number followed by a comma like {m,} matches m or more occur-
  79. rences.  "q{23,}" matches 23 or more consecutive appearances of the letter q. 
  80.  
  81.    7. A string of any of the above items in a row matches a sequence of the
  82. things they match, consecutively.  For instance, "ab*cd[efg]" matches the
  83. letter a followed by zero or more occurrences of the letter b, followed by the
  84. letters c and d, and one of e, f, or g.  (e.g. "abbbcdf" or "acdg")
  85.  
  86.    8. In general, an expression can match a substring anywhere within a line
  87. in a file.  However, if the first character of the expression is a caret (^^),
  88. then the substring must be at the start of a line, while if the expression
  89. ends with a dollar sign ($), it must be at the end.  Hence, "^^abc" matches the
  90. string "abc" only if it occurs at the beginning of a line; "^^def$" matches the
  91. string "def" only if it is an entire line by itself.
  92.  
  93.    Some applications:  If you have a list of friends' names and phone numbers
  94. in a file PHONE.DAT, the command
  95. ^Cgrep -i john phone.dat
  96. will give you the line(s) containing the phone numbers of anyone named John or 
  97. Johnson.  (The -i is there to ignore upper and lower case distinctions.) 
  98.  
  99.    If you want to know how often the word "indisputably" is used in the text
  100. file VERBOSE.TXT, the command
  101. ^Cgrep -ci indisputably verbose.txt
  102. will tell you.  (Note that only the number of lines containing the word is
  103. given, not necessarily the number of times the word appears.  If the word
  104. appears more than once in a line, the number will be underreported.)
  105.  
  106.    If you're trying to find "inalienable" or "unalienable" in the Declaration
  107. of Independence (but you're not sure which), you might try
  108. ^Cgrep [ui]nalienable declarat.ion
  109.  
  110.    To look for a text line containing the word "whatever" followed somewhere
  111. later by the word "want", with arbitrary text in between, try
  112. ^Cgrep whatever.*want *.txt
  113.  
  114.    If you forgot where on your hard disk you had a file that talked about
  115. Joe Finkelstein, you can type
  116. ^Cgrep -id finkelstein \*.*
  117.  
  118.    There are lots of other applications.  Use GREP any time you need to find
  119. something that's buried in a disk file.
  120.  
  121.    Please note that this program runs faster the fewer special characters (like
  122. the asterisk) you use.  Searches for a pure character string with no special
  123. stuff go fairly fast, while searches for a string beginning with ".*" (worst
  124. case) can be very slow.  Plan your input accordingly.  Also, of course, the more
  125. files you search the longer it will take.  Narrow it down if possible by
  126. selecting a subdirectory and extension, like \subdir\*.bas instead of searching
  127. all files from the root down with \*.*.
  128.  
  129. DISK FILES THIS PROGRAM USES:
  130. ^FGREP.EXE
  131.