[BACK]Return to grep.1 CVS log [TXT][DIR] Up to [local] / src / usr.bin / grep

Annotation of src/usr.bin/grep/grep.1, Revision 1.32

1.32    ! jmc         1: .\"    $OpenBSD: grep.1,v 1.31 2005/06/05 19:40:22 jmc Exp $
1.6       deraadt     2: .\" Copyright (c) 1980, 1990, 1993
                      3: .\"    The Regents of the University of California.  All rights reserved.
1.1       deraadt     4: .\"
                      5: .\" Redistribution and use in source and binary forms, with or without
                      6: .\" modification, are permitted provided that the following conditions
                      7: .\" are met:
                      8: .\" 1. Redistributions of source code must retain the above copyright
                      9: .\"    notice, this list of conditions and the following disclaimer.
                     10: .\" 2. Redistributions in binary form must reproduce the above copyright
                     11: .\"    notice, this list of conditions and the following disclaimer in the
                     12: .\"    documentation and/or other materials provided with the distribution.
1.9       millert    13: .\" 3. Neither the name of the University nor the names of its contributors
1.6       deraadt    14: .\"    may be used to endorse or promote products derived from this software
                     15: .\"    without specific prior written permission.
1.1       deraadt    16: .\"
1.6       deraadt    17: .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1.1       deraadt    18: .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     19: .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.6       deraadt    20: .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1.1       deraadt    21: .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     22: .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     23: .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     24: .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     25: .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     26: .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     27: .\" SUCH DAMAGE.
                     28: .\"
1.6       deraadt    29: .\"    @(#)grep.1      8.3 (Berkeley) 4/18/94
                     30: .\"
                     31: .Dd July 3, 1999
1.1       deraadt    32: .Dt GREP 1
                     33: .Os
                     34: .Sh NAME
1.27      jmc        35: .Nm grep , egrep , fgrep ,
                     36: .Nm zgrep , zegrep , zfgrep
1.6       deraadt    37: .Nd file pattern searcher
1.1       deraadt    38: .Sh SYNOPSIS
                     39: .Nm grep
1.27      jmc        40: .Bk -words
                     41: .Op Fl abcEFGHhIiLlnoPqRSsUVvwxZ
                     42: .Op Fl A Ar num
                     43: .Op Fl B Ar num
                     44: .Op Fl C Ns Op Ar num
                     45: .Op Fl e Ar pattern
                     46: .Op Fl f Ar file
                     47: .Op Fl -binary-files Ns = Ns Ar value
1.12      tedu       48: .Op Fl -context Ns Op = Ns Ar num
1.24      otto       49: .Op Fl -line-buffered
1.21      mcbride    50: .Op Ar pattern
1.6       deraadt    51: .Op Ar
1.27      jmc        52: .Ek
1.1       deraadt    53: .Sh DESCRIPTION
                     54: The
1.6       deraadt    55: .Nm grep
1.27      jmc        56: utility searches any given input files,
                     57: selecting lines that match one or more patterns.
                     58: By default, a pattern matches an input line if the regular expression
                     59: (RE) in the pattern matches the input line
                     60: without its trailing newline.
1.6       deraadt    61: An empty expression matches every line.
                     62: Each input line that matches at least one of the patterns is written
                     63: to the standard output.
1.1       deraadt    64: .Pp
1.6       deraadt    65: .Nm grep
1.27      jmc        66: is used for simple patterns and
1.29      jmc        67: basic regular expressions
                     68: .Pq BREs ;
1.1       deraadt    69: .Nm egrep
1.29      jmc        70: can handle extended regular expressions
                     71: .Pq EREs .
                     72: See
                     73: .Xr re_format 7
                     74: for more information on regular expressions.
1.1       deraadt    75: .Nm fgrep
1.27      jmc        76: is quicker than both
                     77: .Nm grep
                     78: and
                     79: .Nm egrep ,
                     80: but can only handle fixed patterns
                     81: (i.e. it does not interpret regular expressions).
                     82: Patterns may consist of one or more lines,
                     83: allowing any of the pattern lines to match a portion of the input.
                     84: .Pp
1.7       deraadt    85: .Nm zgrep ,
                     86: .Nm zegrep ,
                     87: and
                     88: .Nm zfgrep
1.27      jmc        89: act like
1.7       deraadt    90: .Nm grep ,
                     91: .Nm egrep ,
                     92: and
1.27      jmc        93: .Nm fgrep ,
                     94: respectively, but accept input files compressed with the
1.6       deraadt    95: .Xr compress 1
                     96: or
                     97: .Xr gzip 1
                     98: compression utilities.
1.1       deraadt    99: .Pp
1.6       deraadt   100: The following options are available:
                    101: .Bl -tag -width indent
                    102: .It Fl A Ar num
1.7       deraadt   103: Print
1.6       deraadt   104: .Ar num
                    105: lines of trailing context after each match.
1.27      jmc       106: See also the
                    107: .Fl B
                    108: and
                    109: .Fl C
                    110: options.
                    111: .It Fl a
                    112: Treat all files as ASCII text.
1.30      jmc       113: Normally
                    114: .Nm
                    115: will simply print
                    116: .Dq Binary file ... matches
                    117: if files contain binary characters.
                    118: Use of this option forces
                    119: .Nm
                    120: to output lines matching the specified pattern.
1.6       deraadt   121: .It Fl B Ar num
1.7       deraadt   122: Print
                    123: .Ar num
1.6       deraadt   124: lines of leading context before each match.
1.27      jmc       125: See also the
                    126: .Fl A
                    127: and
                    128: .Fl C
                    129: options.
                    130: .It Fl b
                    131: The offset in bytes of a matched pattern is
                    132: displayed in front of the respective matched line.
                    133: .It Fl C Ns Op Ar num
                    134: Print
                    135: .Ar num
                    136: lines of leading and trailing context surrounding each match.
                    137: The default is 2 and is equivalent to
1.6       deraadt   138: .Fl A
                    139: .Ar 2
                    140: .Fl B
                    141: .Ar 2 .
1.27      jmc       142: Note:
                    143: no whitespace may be given between the option and its argument.
                    144: .It Fl c
                    145: Only a count of selected lines is written to standard output.
1.1       deraadt   146: .It Fl E
1.27      jmc       147: Interpret
                    148: .Ar pattern
                    149: as an extended regular expression
                    150: (i.e. force
1.7       deraadt   151: .Nm grep
                    152: to behave as
1.27      jmc       153: .Nm egrep ) .
                    154: .It Fl e Ar pattern
                    155: Specify a pattern used during the search of the input:
                    156: an input line is selected if it matches any of the specified patterns.
                    157: This option is most useful when multiple
                    158: .Fl e
                    159: options are used to specify multiple patterns,
                    160: or when a pattern begins with a dash
                    161: .Pq Sq - .
1.1       deraadt   162: .It Fl F
1.27      jmc       163: Interpret
                    164: .Ar pattern
                    165: as a set of fixed strings
                    166: (i.e. force
1.6       deraadt   167: .Nm grep
1.7       deraadt   168: to behave as
1.27      jmc       169: .Nm fgrep ) .
                    170: .It Fl f Ar file
                    171: Read one or more newline separated patterns from
                    172: .Ar file .
                    173: Newlines are not considered part of a pattern.
1.6       deraadt   174: .It Fl G
1.27      jmc       175: Interpret
                    176: .Ar pattern
                    177: as a basic regular expression
                    178: (i.e. force
1.6       deraadt   179: .Nm grep
1.27      jmc       180: to behave as traditional
                    181: .Nm grep ) .
1.6       deraadt   182: .It Fl H
1.7       deraadt   183: If
1.6       deraadt   184: .Fl R
1.20      jmc       185: is specified, follow symbolic links only if they were explicitly listed
1.6       deraadt   186: on the command line.
1.27      jmc       187: The default is not to follow symbolic links.
                    188: .It Fl h
                    189: Never print filename headers
                    190: .Pq i.e. filenames
                    191: with output lines.
1.10      tedu      192: .It Fl I
                    193: Ignore binary files.
1.27      jmc       194: .It Fl i
                    195: Perform case insensitive matching.
                    196: By default,
                    197: .Nm grep
                    198: is case sensitive.
1.6       deraadt   199: .It Fl L
                    200: Only the names of files not containing selected lines are written to
                    201: standard output.
                    202: Pathnames are listed once per file searched.
1.28      jmc       203: If the standard input is searched, the string
                    204: .Dq (standard input)
1.6       deraadt   205: is written.
1.1       deraadt   206: .It Fl l
1.6       deraadt   207: Only the names of files containing selected lines are written to
1.7       deraadt   208: standard output.
1.27      jmc       209: .Nm grep
                    210: will only search a file until a match has been found,
                    211: making searches potentially less expensive.
1.7       deraadt   212: Pathnames are listed once per file searched.
1.28      jmc       213: If the standard input is searched, the string
                    214: .Dq (standard input)
1.6       deraadt   215: is written.
1.1       deraadt   216: .It Fl n
1.6       deraadt   217: Each output line is preceded by its relative line number in the file,
                    218: starting at line 1.
                    219: The line number counter is reset for each file processed.
                    220: This option is ignored if
                    221: .Fl c ,
1.27      jmc       222: .Fl L ,
1.6       deraadt   223: .Fl l ,
                    224: or
1.32    ! jmc       225: .Fl q
1.6       deraadt   226: is
                    227: specified.
                    228: .It Fl o
                    229: Always print filename headers with output lines.
1.27      jmc       230: .It Fl P
                    231: If
                    232: .Fl R
                    233: is specified, no symbolic links are followed.
                    234: This is the default.
1.1       deraadt   235: .It Fl q
1.27      jmc       236: Quiet mode:
                    237: suppress normal output.
                    238: .Nm grep
                    239: will only search a file until a match has been found,
                    240: making searches potentially less expensive.
                    241: .It Fl R
                    242: Recursively search subdirectories listed.
                    243: .It Fl S
                    244: If
                    245: .Fl R
                    246: is specified, all symbolic links are followed.
                    247: The default is not to follow symbolic links.
1.1       deraadt   248: .It Fl s
1.7       deraadt   249: Silent mode.
1.27      jmc       250: Nonexistent and unreadable files are ignored
                    251: (i.e. their error messages are suppressed).
                    252: .It Fl U
                    253: Search binary files, but do not attempt to print them.
                    254: .It Fl V
                    255: Display version information.
                    256: All other options are ignored.
1.1       deraadt   257: .It Fl v
1.6       deraadt   258: Selected lines are those
                    259: .Em not
1.14      dhartmei  260: matching any of the specified patterns.
1.1       deraadt   261: .It Fl w
1.27      jmc       262: The expression is searched for as a word (as if surrounded by
                    263: .Sq [[:<:]]
                    264: and
                    265: .Sq [[:>:]] ;
                    266: see
1.26      jmc       267: .Xr re_format 7 ) .
1.1       deraadt   268: .It Fl x
1.6       deraadt   269: Only input lines selected against an entire fixed string or regular
                    270: expression are considered to be matching lines.
1.27      jmc       271: .It Fl Z
                    272: Force
                    273: .Nm grep
                    274: to behave as
                    275: .Nm zgrep .
                    276: .It Fl Fl binary-files Ns = Ns Ar value
                    277: Controls searching and printing of binary files.
                    278: Options are
                    279: .Ar binary ,
                    280: the default: search binary files but do not print them;
                    281: .Ar without-match :
                    282: do not search binary files;
                    283: and
                    284: .Ar text :
                    285: treat all files as text.
1.12      tedu      286: .Sm off
                    287: .It Fl Fl context Op = Ar num
                    288: .Sm on
                    289: Print
                    290: .Ar num
                    291: lines of leading and trailing context.
1.27      jmc       292: The default is 2.
1.24      otto      293: .It Fl Fl line-buffered
                    294: Force output to be line buffered.
                    295: By default, output is line buffered when standard output is a terminal
                    296: and block buffered otherwise.
1.6       deraadt   297: .Pp
1.1       deraadt   298: .El
1.6       deraadt   299: If no file arguments are specified, the standard input is used.
1.7       deraadt   300: .Sh RETURN VALUES
1.1       deraadt   301: The
1.6       deraadt   302: .Nm grep
1.1       deraadt   303: utility exits with one of the following values:
                    304: .Pp
                    305: .Bl -tag -width flag -compact
1.6       deraadt   306: .It Li 0
                    307: One or more lines were selected.
                    308: .It Li 1
                    309: No lines were selected.
1.27      jmc       310: .It Li \*(Gt1
1.6       deraadt   311: An error occurred.
1.4       jmc       312: .El
1.6       deraadt   313: .Sh EXAMPLES
1.27      jmc       314: To find all occurrences of the word
                    315: .Sq patricia
                    316: in a file:
1.6       deraadt   317: .Pp
1.27      jmc       318: .Dl $ grep 'patricia' myfile
1.6       deraadt   319: .Pp
                    320: To find all occurrences of the pattern
1.27      jmc       321: .Ql .Pp
1.6       deraadt   322: at the beginning of a line:
                    323: .Pp
1.27      jmc       324: .Dl $ grep '^\e.Pp' myfile
1.6       deraadt   325: .Pp
1.7       deraadt   326: The apostrophes ensure the entire expression is evaluated by
1.6       deraadt   327: .Nm grep
1.27      jmc       328: instead of by the user's shell.
1.6       deraadt   329: The caret
1.27      jmc       330: .Ql ^
1.6       deraadt   331: matches the null string at the beginning of a line,
                    332: and the
1.27      jmc       333: .Ql \e
1.6       deraadt   334: escapes the
1.27      jmc       335: .Ql \&. ,
1.6       deraadt   336: which would otherwise match any character.
1.21      mcbride   337: .Pp
1.27      jmc       338: To find all lines in a file which do not contain the words
                    339: .Sq foo
                    340: or
                    341: .Sq bar :
1.21      mcbride   342: .Pp
1.27      jmc       343: .Dl $ grep -v -e 'foo' -e 'bar' myfile
1.6       deraadt   344: .Pp
                    345: A simple example of an extended regular expression:
                    346: .Pp
1.27      jmc       347: .Dl $ egrep '19|20|25' calendar
1.6       deraadt   348: .Pp
1.27      jmc       349: Peruses the file
                    350: .Sq calendar
                    351: looking for either 19, 20, or 25.
1.1       deraadt   352: .Sh SEE ALSO
1.6       deraadt   353: .Xr ed 1 ,
                    354: .Xr ex 1 ,
1.27      jmc       355: .Xr gzip 1 ,
1.6       deraadt   356: .Xr sed 1 ,
1.7       deraadt   357: .Xr re_format 7
1.27      jmc       358: .Sh STANDARDS
                    359: The
                    360: .Nm grep
                    361: utility is compliant with the
1.31      jmc       362: .St -p1003.1-2004
1.27      jmc       363: specification.
                    364: .Pp
                    365: The flags
                    366: .Op Fl AaBbCGHhILoPRSUVwZ
                    367: are extensions to that specification.
                    368: .Pp
                    369: All long options are provided for compatibility with
                    370: GNU versions of this utility.
                    371: .Pp
                    372: Historic versions of the
                    373: .Nm grep
                    374: utility also supported the flags
                    375: .Op Fl ruy .
                    376: This implementation supports those options;
                    377: however, their use is strongly discouraged.
1.1       deraadt   378: .Sh HISTORY
1.6       deraadt   379: The
1.1       deraadt   380: .Nm grep
1.27      jmc       381: command first appeared in
1.1       deraadt   382: .At v6 .