home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / fileutil / tcggrep2.arj / GREP.DOC < prev    next >
Encoding:
Text File  |  1991-01-08  |  7.7 KB  |  156 lines

  1. GREP(1)                  USER COMMANDS                    GREP(1)
  2.  
  3. NAME
  4.      grep, egrep - print lines matching a regular expression
  5.  
  6. SYNOPSIS
  7.      grep [ -CVbchilnsvwx ] [ -num ] [ -AB num ] [ [ -e ] expr  |
  8.      -f file ] [ files ... ]
  9.  
  10. DESCRIPTION
  11.      Grep searches the files listed in the arguments (or standard
  12.      input  if  no  files are given) for all lines that contain a
  13.      match for the given expr.  If  any  lines  match,  they  are
  14.      printed.
  15.  
  16.      Also, if any matches were  found,  grep  will  exit  with  a
  17.      status  of 0, but if no matches were found it will exit with
  18.      a status of 1.  This is useful for  building  shell  scripts
  19.      that use grep as a condition for, for example, the if state-
  20.      ment.
  21.  
  22.      When invoked as egrep the syntax of  the  expr  is  slightly
  23.      different; See below.
  24.  
  25. REGULAR EXPRESSIONS
  26.           (grep)    (egrep)   (explanation)
  27.  
  28.           c         c         a   single   (non-meta)   character
  29.                               matches itself.
  30.           .         .         matches any single character except
  31.                               newline.
  32.           \?        ?         postfix operator;  preceeding  item
  33.                               is optional. EG. ab\?c matches abc
  34.                               or ab
  35.           *         *         postfix operator; preceeding item 0
  36.                               or more times.
  37.           \+        +         postfix operator; preceeding item 1
  38.                               or more times.
  39.           \|        |         infix  operator;   matches   either
  40.                               argument. Eg. ha\|it matches hat
  41.                               and hit.
  42.           ^         ^         matches the  empty  string  at  the
  43.                               beginning of a line.
  44.           $         $         matches the empty string at the end
  45.                               of a line.
  46.           \<        \<        matches the  empty  string  at  the
  47.                               beginning of a word. Eg. \<int will
  48.                               find words beginning with 'int', but
  49.                               not 'printf'.
  50.           \>        \>        matches the empty string at the end
  51.                               of a word. Eg. int\> will find the
  52.                               word 'print' but not 'integer'.
  53.           [chars]   [chars]   match any character  in  the  given
  54.                               class; if the first character after
  55.                               [ is ^, match any character not  in
  56.                               the given class; a range of charac-
  57.                               ters may be specified by first-last
  58.                               for example, \W (below) is equi-
  59.                               valent to the class [^A-Za-z0-9_]
  60.           \( \)     ( )       parentheses are  used  to  override
  61.                               operator precedence. This allows
  62.                               you to create sub-expressions:
  63.                               f\(orma\)\|\(oo\)t matches either
  64.                               'format' or 'foot'.
  65.           \digit    \digit    \n matches a  repeat  of  the  text
  66.                               matched  earlier  in  the regexp by
  67.                               the subexpression  inside  the  nth
  68.                               opening parenthesis. Eg:
  69.                               \(b[ae]t\)\1 would match 'batbat'
  70.                               or 'betbet' but not 'betbat' or
  71.                               'batbet'.
  72.           \         \         any special character may  be  pre-
  73.                               ceded  by  a  backslash to match it
  74.                               literally.
  75.  
  76.           \b        \b        matches the  empty  string  at  the
  77.                               edge of a word. The same effect as
  78.                               \< or \> (depending on which end of
  79.                               the word itappears on). Eg. int\b is
  80.                               the same as int\>, \bint is the same
  81.                               as \<int.
  82.           \B        \B        matches the empty string if not  at
  83.                               the edge of a word. Eg. \Bint\B would
  84.                               match 'printf', but not 'print' or
  85.                               'integer'.
  86.           \w        \w        matches word-constituent characters
  87.                               (letters & digits or underscore).
  88.                               Saves typing [a-zA-Z0-9_].
  89.           \W        \W        matches  characters  that  are  not
  90.                               word-constituent. Saves typing
  91.                               [^a-zA-Z0-9_].
  92.      Operator precedence is (highest to lowest) ?, *, and +, con-
  93.      catenation, and finally |.  All other constructs are syntac-
  94.      tically identical  to  normal  characters. Use parentheses
  95.      to override this.
  96.  
  97. OPTIONS
  98.      -A num
  99.           print <num> lines of context after every matching line
  100.      -B num
  101.           print num lines of context before every matching line
  102.      -b   print every match preceded by its byte offset
  103.      -C   print 2 lines of context on each side of every match
  104.      -c   print a total count of matching lines only
  105.      -num print num lines of context on each side of every match
  106.      -d 'expr'
  107.           removes the surrounding quotes from the <expr>, then
  108.           acts as -e, below. Useful to prevent wild card file
  109.           name expansion from taking place on <expr>.
  110.      -e expr
  111.           search for <expr> useful if expr begins with '-'
  112.      -f file
  113.           search for the expression contained in file - useful
  114.           for keeping a permanent copy of complicated but handy
  115.           search patterns.          
  116.      -h   don't display filenames on matches
  117.      -H   display filenames on matches - this will force file name
  118.           output even if grepping a single file.
  119.      -i   ignore case difference when comparing strings
  120.      -I   do not ignore case difference when comparing strings
  121.      -l   list files containing matches only
  122.      -n   print each match preceded by its line number
  123.      -N   omit line numbers on matches
  124.      -s   run silently producing no output except error messages
  125.           Useful when executing grep in a makefile, etc.
  126.      -V   print the version number on stderr
  127.      -v   print only lines that contain no matches for the <expr>
  128.      -w   print only lines where the match is a complete word
  129.           Eg. grep -w print would find lines containing the word
  130.           'print' but not lines containing 'printf'
  131.      -x   print only lines where the match is a whole line
  132.           Ie. grep -x int would only find lines which have only
  133.           the single word int (no leading or trailing whitespace).
  134.  
  135. You can set an environment variable GREP_OPTS to supply these flags.
  136. In particular, if you always want case insensitive searches and line
  137. numbers, set GREP_OPTS=-in. The only way to override these settings
  138. is
  139. set GREP_OPTS=
  140. to remove the string. The flags must appear as a single string with
  141. a leading '-'. Do not set GREP_OPTS=-i -n, it will not work.  
  142.  
  143. For users of the MKS toolkit, a second environment variable MKS can be
  144. used to suprress globbing and to use the MKS argument passing
  145. convention (arguments are passed in the environment, flagged with
  146. a leading '~'). To use this option, define an environment variable MKS
  147. (the definition does not matter, only the existence of the variable).
  148.  
  149. A third environment variable, NO_GLOB, can be defined. If this variable
  150. is defined, ?grep will not glob filenames (useful for other *nix like
  151. shells which do file globbing). The MKS variable, above, suppresses
  152. globbing regardless of the existence/non-existence of NO_GLOB.
  153.  
  154.  
  155.  
  156.