386BSD 0.1 development
[unix-history] / usr / src / usr.bin / grep / grep.1
CommitLineData
da92d036
WJ
1.TH GREP 1 "1988 December 13" "GNU Project"
2.UC 4
3.SH NAME
4grep, egrep \- print lines matching a regular expression
5.SH SYNOPSIS
6.B grep
7[
8.B \-CVbchilnsvwx
9] [
10.BI \- num
11] [
12.B \-AB
13.I num
14] [ [
15.B \-e
16]
17.I expr
18|
19.B \-f
20.I file
21] [
22.I "files ..."
23]
24.SH DESCRIPTION
25.I Grep
26searches the files listed in the arguments (or standard
27input if no files are given) for all lines that contain a match for
28the given
29.IR expr .
30If any lines match, they are printed.
31.PP
32Also, if any matches were found,
33.I grep
34will exit with a status of 0, but if no matches were found it will exit
35with a status of 1. This is useful for building shell scripts that
36use
37.I grep
38as a condition for, for example, the
39.I if
40statement.
41.PP
42When invoked as
43.I egrep
44the syntax of the
45.I expr
46is slightly different; See below.
47.br
48.SH "REGULAR EXPRESSIONS"
49.RS 2.5i
50.ta 1i; 2i
51.sp
52.ti -2.0i
53(grep) (egrep) (explanation)
54.sp
55.ti -2.0i
56\fIc\fP \fIc\fP a single (non-meta) character matches itself.
57.sp
58.ti -2.0i
59\&. . matches any single character except newline.
60.sp
61.ti -2.0i
62\\? ? postfix operator; preceeding item is optional.
63.sp
64.ti -2.0i
65\(** \(** postfix operator; preceeding item 0 or
66more times.
67.sp
68.ti -2.0i
69\\+ + postfix operator; preceeding item 1 or
70more times.
71.sp
72.ti -2.0i
73\\| | infix operator; matches either
74argument.
75.sp
76.ti -2.0i
77^ ^ matches the empty string at the beginning of a line.
78.sp
79.ti -2.0i
80$ $ matches the empty string at the end of a line.
81.sp
82.ti -2.0i
83\\< \\< matches the empty string at the beginning of a word.
84.sp
85.ti -2.0i
86\\> \\> matches the empty string at the end of a word.
87.sp
88.ti -2.0i
89[\fIchars\fP] [\fIchars\fP] match any character in the given class; if the
90first character after [ is ^, match any character
91not in the given class; a range of characters may
92be specified by \fIfirst\-last\fP; for example, \\W
93(below) is equivalent to the class [^A\-Za\-z0\-9]
94.sp
95.ti -2.0i
96\\( \\) ( ) parentheses are used to override operator precedence.
97.sp
98.ti -2.0i
99\\\fIdigit\fP \\\fIdigit\fP \\\fIn\fP matches a repeat of the text
100matched earlier in the regexp by the subexpression inside the nth
101opening parenthesis.
102.sp
103.ti -2.0i
104\\ \\ any special character may be preceded
105by a backslash to match it literally.
106.sp
107.ti -2.0i
108(the following are for compatibility with GNU Emacs)
109.sp
110.ti -2.0i
111\\b \\b matches the empty string at the edge of a word.
112.sp
113.ti -2.0i
114\\B \\B matches the empty string if not at the edge of a word.
115.sp
116.ti -2.0i
117\\w \\w matches word-constituent characters (letters & digits).
118.sp
119.ti -2.0i
120\\W \\W matches characters that are not word-constituent.
121.RE
122.PP
123Operator precedence is (highest to lowest) ?, \(**, and +, concatenation,
124and finally |. All other constructs are syntactically identical to
125normal characters. For the truly interested, the file dfa.c describes
126(and implements) the exact grammar understood by the parser.
127.SH OPTIONS
128.TP
129.BI \-A " num"
130print <num> lines of context after every matching line
131.TP
132.BI \-B " num"
133print
134.I num
135lines of context before every matching line
136.TP
137.B \-C
138print 2 lines of context on each side of every match
139.TP
140.BI \- num
141print
142.I num
143lines of context on each side of every match
144.TP
145.B \-V
146print the version number on the diagnostic output
147.TP
148.B \-b
149print every match preceded by its byte offset
150.TP
151.B \-c
152print a total count of matching lines only
153.TP
154.BI \-e " expr"
155search for
156.IR expr ;
157useful if
158.I expr
159begins with \-
160.TP
161.BI \-f " file"
162search for the expression contained in
163.I file
164.TP
165.B \-h
166don't display filenames on matches
167.TP
168.B \-i
169ignore case difference when comparing strings
170.TP
171.B \-l
172list files containing matches only
173.TP
174.B \-n
175print each match preceded by its line number
176.TP
177.B \-s
178run silently producing no output except error messages
179.TP
180.B \-v
181print only lines that contain no matches for the <expr>
182.TP
183.B \-w
184print only lines where the match is a complete word
185.TP
186.B \-x
187print only lines where the match is a whole line
188.SH "SEE ALSO"
189emacs(1), ed(1), sh(1),
190.I "GNU Emacs Manual"
191.SH INCOMPATIBILITIES
192The following incompatibilities with UNIX
193.I grep
194exist:
195.PP
196.RS 0.5i
197The context-dependent meaning of \(** is not quite the same (grep only).
198.PP
199.B \-b
200prints a byte offset instead of a block offset.
201.PP
202The {\fIm,n\fP} construct of System V grep is not implemented.
203.PP
204.SH BUGS
205GNU \fIe?grep\fP has been thoroughly debugged and tested by several people
206over a period of several months; we think it's a reliable beast or we
207wouldn't distribute it. If by some fluke of the universe you discover
208a bug, send a detailed description (including options, regular
209expressions, and a copy of an input file that can reproduce it) to me,
210mike@wheaties.ai.mit.edu.
211.PP
212There is also a newsgroup, gnu.utils.bug, for reporting FSF utility
213programs' bugs and fixes; but before reporting something as a bug,
214please try to be sure that it really is a bug, not a misunderstanding
215or a deliberate feature. Also, include the version number of the
216utility program you are running in \fIevery\fR bug report that you
217send in. Please do not send anything but bug reports to this
218newsgroup.
219.PP
220.SH AVAILABILITY
221.PP
222GNU
223.I grep
224is free; anyone may redistribute copies of
225.I grep
226to
227anyone under the terms stated in the
228GNU General Public License,
229a copy of which may be found in each copy of
230.IR "GNU Emacs" .
231See also the comment at the beginning of the source code file grep.c.
232.PP
233Copies of GNU
234.I grep
235may sometimes be received packaged with distributions of Unix systems,
236but it is never included in the scope of any license covering those
237systems. Such inclusion violates the terms on which distribution
238is permitted. In fact, the primary purpose of the General Public
239License is to prohibit anyone from attaching any other restrictions
240to redistribution of any of the Free Software Foundation programs.
241.SH AUTHORS
242Mike Haertel wrote the deterministic regexp code and the bulk
243of the program.
244.PP
245James A. Woods is responsible for the hybridized search strategy
246of using Boyer-Moore-Gosper fixed-string search as a filter
247before calling the general regexp matcher.
248.PP
249Arthur David Olson contributed code that finds fixed strings for
250the aforementioned BMG search for a large class of regexps.
251.PP
252Richard Stallman wrote the backtracking regexp matcher that is
253used for \\\fIdigit\fP backreferences, as well as the getopt that
254is provided for 4.2BSD sites. The backtracking matcher was
255originally written for GNU Emacs.
256.PP
257D. A. Gwyn wrote the C alloca emulation that is provided so
258System V machines can run this program. (Alloca is used only
259by RMS' backtracking matcher, and then only rarely, so there
260is no loss if your machine doesn't have a "real" alloca.)
261.PP
262Scott Anderson and Henry Spencer designed the regression tests
263used in the "regress" script.
264.PP
265Paul Placeway wrote the original version of this manual page.