Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
GREP(1)                    OpenBSD Reference Manual                    GREP(1)

NAME
     grep, egrep, fgrep, zgrep, zegrep, zfgrep - file pattern searcher

SYNOPSIS
     grep [-AB num] [-CEFGHILPRSUVZabchilnoqsvwx] [--context[=num]]
          [--binary-files=value] [-e pattern] [-f file] [file ...]

DESCRIPTION
     The grep utilities search the given input files selecting lines that
     match one or more patterns.  By default, a pattern matches an input line
     if any regular expression (RE) in the pattern matches the input line
     without its trailing newline.  An empty expression matches every line.
     Each input line that matches at least one of the patterns is written to
     the standard output.

     The grep utility is used for simple patterns and ex(1) or ed(1) style
     regular expressions.  The egrep utility can handle extended regular ex-
     pressions and multi-line patterns.  The fgrep utility is quick but can
     handle only fixed patterns consisting of one or more lines, allowing any
     of the pattern lines to match a portion of the input.  The zgrep, zegrep,
     and zfgrep utilities act like grep, egrep, and fgrep respectively but ac-
     cept input files compressed with the compress(1) or gzip(1) compression
     utilities.

     The following options are available:

     -A num  Print num lines of trailing context after each match.

     -B num  Print num lines of leading context before each match.

     -C      Print 2 lines of trailing context after each match.  Equivalent
             to -A 2 -B 2.

     -E      Force grep to behave as egrep.

     -F      Force grep to behave as fgrep.

     -G      Force grep to behave as grep.

     -H      If -R is specified, follow symbolic links only if they were ex-
             plictly listed on the command line.

     -I      Ignore binary files.

     -L      Only the names of files not containing selected lines are written
             to standard output.  Pathnames are listed once per file searched.
             If the standard input is searched, the pathname `-' is written.

     -P      If -R is specified, no symbolic links are followed.

     -R      Recursively search subdirectories listed.

     -S      If -R is specified, all symbolic links are followed.

     -U      Search binary files, but do not attempt to print them.

     -V      Display version information.

     -Z      Force grep to behave as zgrep.

     -a      Treat all files as text.

     -b      The block number on the disk in which a matched pattern is locat-
             ed is displayed in front of the respective matched line.

     -c      Only a count of selected lines is written to standard output.

     -e expression
             Specify a pattern used during the search of the input.  Multiple
             -e options can be used to specify multiple patterns; an input
             line is selected if it matches any of the specified patterns.

     -f pattern_file
             Read one or more newline separated patterns from pattern_file.
             Newlines are not considered part of a pattern.

     -h      Never print filename headers with output lines.

     -i      Perform case insensitive matching.

     -l      Only the names of files containing selected lines are written to
             standard output.  Pathnames are listed once per file searched.
             If the standard input is searched, the pathname `-' is written.

     -n      Each output line is preceded by its relative line number in the
             file, starting at line 1.  The line number counter is reset for
             each file processed.  This option is ignored if -c, -l, or -q is
             specified.

     -o      Always print filename headers with output lines.

     -q      Suppress normal output.

     -s      Silent mode.  Nonexistent and unreadable files are ignored.

     -v      Selected lines are those not matching any of the specified pat-
             terns.

     -w      The expression is searched for as a word (as if surrounded by
             `\<' and `\>', see ex(1)).

     -x      Only input lines selected against an entire fixed string or regu-
             lar expression are considered to be matching lines.

     --context[=num]
             Print num lines of leading and trailing context.  Default is 2.

     --binary-files=value
             Controls searching and printing of binary files.  Options are
             binary, the default, search binary files but do not print them;
             without-match, do not search binary files; and text, treat all
             files as text.

     If no file arguments are specified, the standard input is used.

RETURN VALUES
     The grep utility exits with one of the following values:

     0     One or more lines were selected.
     1     No lines were selected.
     >1    An error occurred.

EXTENDED REGULAR EXPRESSIONS
     The following characters are interpreted by egrep:

     $     Align the match from the end of the line.
     ^     Align the match from the beginning of the line.
     |     Add another pattern (see example below).
     ?     Match 1 or less sequential repetitions of the pattern.
     +     Match 1 or more sequential repetitions of the pattern.
     *     Match 0 or more sequential repetitions of the pattern.
     {}    Match specified number of sequential repetitions of the pattern.
     []    Match any single character or range of characters enclosed in the
           brackets.
     \     Escape special characters which have meaning to egrep, the set of
           {$,.,^,[,],|,?,+,*,{,},(,)}.

EXAMPLES
     To find all occurrences of the word patricia in a file:

           grep patricia myfile

     To find all occurrences of the pattern `.Pp' at the beginning of a line:

           grep '^\.Pp'

     The apostrophes ensure the entire expression is evaluated by grep instead
     of by the user's shell.  The caret `^' matches the null string at the be-
     ginning of a line, and the `\' escapes the `.' which would otherwise
     match any character.

     A simple example of an extended regular expression:

           egrep '19|20|25' calendar

     Peruses the file calendar looking for either 19, 20 or 25.

SEE ALSO
     ed(1), ex(1), sed(1), regex(3), regexp(3), re_format(7)

HISTORY
     The grep command appeared in Version 6 AT&T UNIX.

OpenBSD 3.4                      July 3, 1999                                3

NAME | SYNOPSIS | DESCRIPTION | RETURN VALUES | EXTENDED REGULAR EXPRESSIONS | EXAMPLES | SEE ALSO | HISTORY

Want to link to this manual page? Use this URL:
<https://www.freebsd.org/cgi/man.cgi?query=egrep&sektion=1&manpath=OpenBSD+3.4>

home | help