home *** CD-ROM | disk | FTP | other *** search
- GREP(1) USER COMMANDS GREP(1)
-
- NAME
- grep, egrep - print lines matching a regular expression
-
- SYNOPSIS
- grep [ -CVbchilnsvwx ] [ -num ] [ -AB num ] [ [ -e ] expr |
- -f file ] [ files ... ]
-
- DESCRIPTION
- Grep searches the files listed in the arguments (or standard
- input if no files are given) for all lines that contain a
- match for the given expr. If any lines match, they are
- printed.
-
- Also, if any matches were found, grep will exit with a
- status of 0, but if no matches were found it will exit with
- a status of 1. This is useful for building shell scripts
- that use grep as a condition for, for example, the if state-
- ment.
-
- When invoked as egrep the syntax of the expr is slightly
- different; See below.
-
- REGULAR EXPRESSIONS
- (grep) (egrep) (explanation)
-
- c c a single (non-meta) character
- matches itself.
- . . matches any single character except
- newline.
- \? ? postfix operator; preceeding item
- is optional. EG. ab\?c matches abc
- or ab
- * * postfix operator; preceeding item 0
- or more times.
- \+ + postfix operator; preceeding item 1
- or more times.
- \| | infix operator; matches either
- argument. Eg. ha\|it matches hat
- and hit.
- ^ ^ matches the empty string at the
- beginning of a line.
- $ $ matches the empty string at the end
- of a line.
- \< \< matches the empty string at the
- beginning of a word. Eg. \<int will
- find words beginning with 'int', but
- not 'printf'.
- \> \> matches the empty string at the end
- of a word. Eg. int\> will find the
- word 'print' but not 'integer'.
- [chars] [chars] match any character in the given
- class; if the first character after
- [ is ^, match any character not in
- the given class; a range of charac-
- ters may be specified by first-last
- for example, \W (below) is equi-
- valent to the class [^A-Za-z0-9_]
- \( \) ( ) parentheses are used to override
- operator precedence. This allows
- you to create sub-expressions:
- f\(orma\)\|\(oo\)t matches either
- 'format' or 'foot'.
- \digit \digit \n matches a repeat of the text
- matched earlier in the regexp by
- the subexpression inside the nth
- opening parenthesis. Eg:
- \(b[ae]t\)\1 would match 'batbat'
- or 'betbet' but not 'betbat' or
- 'batbet'.
- \ \ any special character may be pre-
- ceded by a backslash to match it
- literally.
-
- \b \b matches the empty string at the
- edge of a word. The same effect as
- \< or \> (depending on which end of
- the word itappears on). Eg. int\b is
- the same as int\>, \bint is the same
- as \<int.
- \B \B matches the empty string if not at
- the edge of a word. Eg. \Bint\B would
- match 'printf', but not 'print' or
- 'integer'.
- \w \w matches word-constituent characters
- (letters & digits or underscore).
- Saves typing [a-zA-Z0-9_].
- \W \W matches characters that are not
- word-constituent. Saves typing
- [^a-zA-Z0-9_].
- Operator precedence is (highest to lowest) ?, *, and +, con-
- catenation, and finally |. All other constructs are syntac-
- tically identical to normal characters. Use parentheses
- to override this.
-
- OPTIONS
- -A num
- print <num> lines of context after every matching line
- -B num
- print num lines of context before every matching line
- -b print every match preceded by its byte offset
- -C print 2 lines of context on each side of every match
- -c print a total count of matching lines only
- -num print num lines of context on each side of every match
- -d 'expr'
- removes the surrounding quotes from the <expr>, then
- acts as -e, below. Useful to prevent wild card file
- name expansion from taking place on <expr>.
- -e expr
- search for <expr> useful if expr begins with '-'
- -f file
- search for the expression contained in file - useful
- for keeping a permanent copy of complicated but handy
- search patterns.
- -h don't display filenames on matches
- -H display filenames on matches - this will force file name
- output even if grepping a single file.
- -i ignore case difference when comparing strings
- -I do not ignore case difference when comparing strings
- -l list files containing matches only
- -n print each match preceded by its line number
- -N omit line numbers on matches
- -s run silently producing no output except error messages
- Useful when executing grep in a makefile, etc.
- -V print the version number on stderr
- -v print only lines that contain no matches for the <expr>
- -w print only lines where the match is a complete word
- Eg. grep -w print would find lines containing the word
- 'print' but not lines containing 'printf'
- -x print only lines where the match is a whole line
- Ie. grep -x int would only find lines which have only
- the single word int (no leading or trailing whitespace).
-
- You can set an environment variable GREP_OPTS to supply these flags.
- In particular, if you always want case insensitive searches and line
- numbers, set GREP_OPTS=-in. The only way to override these settings
- is
- set GREP_OPTS=
- to remove the string. The flags must appear as a single string with
- a leading '-'. Do not set GREP_OPTS=-i -n, it will not work.
-
- For users of the MKS toolkit, a second environment variable MKS can be
- used to suprress globbing and to use the MKS argument passing
- convention (arguments are passed in the environment, flagged with
- a leading '~'). To use this option, define an environment variable MKS
- (the definition does not matter, only the existence of the variable).
-
- A third environment variable, NO_GLOB, can be defined. If this variable
- is defined, ?grep will not glob filenames (useful for other *nix like
- shells which do file globbing). The MKS variable, above, suppresses
- globbing regardless of the existence/non-existence of NO_GLOB.
-
-
-
-