xref: /original-bsd/usr.bin/grep/egrep/grep.1 (revision c3e32dec)
1.\" Copyright (c) 1980, 1990, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" %sccs.include.redist.roff%
5.\"
6.\"	@(#)grep.1	8.1 (Berkeley) 06/06/93
7.\"
8.Dd
9.Dt GREP 1
10.Os
11.Sh NAME
12.Nm grep
13.Nd file pattern searcher
14.Sh SYNOPSIS
15.Nm grep
16.Op Fl bchilnosvw
17.Op Fl e Ar pattern
18.Op Ar
19.Nm egrep
20.Op Fl bchilnosv
21.Op Fl e Ar pattern
22.Op Fl f Ar pattern_file
23.Op Ar
24.Nm fgrep
25.Op Fl bchilnosvx
26.Op Fl e Ar pattern
27.Op Fl f Ar pattern_file
28.Op Ar
29.Sh DESCRIPTION
30The
31.Nm grep
32utilities search the given input files selecting lines
33which match one or more patterns; the type of patterns is controlled
34by the options specified.
35By default, a pattern
36matches an input line if any regular expression (RE) in the
37pattern matches the input line without its trailing <new-line>.
38A null RE matches every line.
39Each input line that matches at
40least one of the patterns is written to the standard output.
41.Pp
42For simple patterns or
43.Xr ex 1
44or
45.Xr ed 1
46style regular expressions, the
47.Nm grep
48utility is used.
49The
50.Nm egrep
51utility
52can handle extended regular expressions and
53embedded <newline>s in pattern
54.Nm fgrep
55is quick but is designed to handle fixed strings.
56A fixed string
57is a string of characters,
58each character
59is matched only by itself.
60The pattern
61value can consist of multiple lines with
62embedded <newline>s.
63In this case, the <newline>s
64act as alternation characters, allowing any of the
65pattern lines to match a portion of the input.
66.Pp
67The following options are available:
68.Pp
69.Bl -tag -width indent
70.It Fl b
71The block number on the disk in which a matched pattern is located
72is displayed in front of the respective matched line.
73.It Fl c
74Only a count of selected lines is written to standard
75output.
76.It Fl e Ar expression
77Specify a pattern used during the search of the
78input.
79Multiple
80.Fl e
81options can be used to specify
82multiple patterns; an input line is selected of it
83matches any of the specified patterns.
84.It Fl f Ar pattern_file
85The pattern is read from the file named by the
86pathname pattern_file.
87Trailing newlines
88in the pattern_file are ignored.
89.Pf ( Nm Egrep
90and
91.Nm fgrep
92only).
93.It Fl h
94Never print filename headers with output lines.
95.It Fl i
96The case of letters is ignored in making comparisons \- that is, upper and
97lower case are considered identical.
98.It Fl l
99Only the names of files containing selected lines
100are written to standard output.
101Pathnames are
102listed once per file searched.
103If the standard
104input is searched, the pathname
105.Sq Fl
106is written.
107.It Fl n
108Each output line is preceded by its relative line
109number in the file; each file starting at line 1.
110The line number counter is reset for each file processed.
111This option is ignored if
112.Fl c ,
113.Fl l ,
114or
115.Fl s
116is
117specified.
118.It Fl o
119Always print filename headers with output lines.
120.It Fl s
121Silent mode.  Nothing is printed (except error messages).
122This is useful for checking the error status.
123.It Fl v
124Selected lines are those
125.Em not
126matching the specified
127patterns.
128.It Fl x
129Only input lines selected against an entire fixed
130string or regular expression are considered to be
131matching lines.
132.Pf ( Nm Fgrep
133only).
134.It Fl w
135The expression is searched for as a word
136(as if surrounded by `\e<' and `\e>', see
137.Xr ex  1  . )
138.Pf ( Nm Grep
139only)
140.Pp
141.El
142If no file arguments are specified, the
143standard input is used.
144.Pp
145The
146.Nm grep
147utility exits with one of the following values:
148.Pp
149.Bl -tag -width flag -compact
150.It Li 0
151One or more lines were selected.
152.It Li 1
153No lines were selected.
154.It Li >1
155An error occurred.
156.El
157.Sh EXTENDED REGULAR EXPRESSIONS
158The following characters are interpreted by
159.Nm egrep :
160.Pp
161.Bl -tag -width flag -compact
162.It Cm \&$
163Align the match from the end of the line.
164.It Cm \&^
165Align the match from the beginning of the line.
166.It Cm \&|
167Add another pattern (see example below).
168.It Cm \&?
169Match 1 or less sequential repetitions of the pattern.
170.It Cm \&+
171Match 1 or more sequential repetitions of the pattern.
172.It Cm \&*
173Match 0 or more sequential repetitions of the pattern.
174.It Cm \&[]
175Match any single character or range of characters
176enclosed in the brackets.
177.It Cm \&\e
178Escape special characters which have meaning to
179.Nm egrep ,
180the set of {$,.,^,[,],|,?,+,*,(,)}.
181.El
182.Sh EXAMPLES
183To find all occurances of the word patricia in a file:
184.Pp
185.Dl grep patricia myfile
186.Pp
187To find all occurences of the pattern
188.Ql \&.Pp
189at the beginning of a line:
190.Pp
191.Dl grep '^\e.Pp'
192.Pp
193The apostrophys assure the entire expression is evaluated by
194.Nm grep
195instead of by the
196users shell.
197The carat or hat
198.Ql Li \&^
199means
200.Em from the beginning of a line ,
201and the
202.Ql Li \&\e
203escapes the
204.Ql Li \&.
205which would otherwise match any character.
206.Pp
207A simple example of an extended regular expression:
208.Pp
209.Dl egrep '19|20|25' calendar
210.Pp
211Peruses the file calendar looking for either 19, 20
212or 25.
213.Sh SEE ALSO
214.Xr ed 1 ,
215.Xr ex 1 ,
216.Xr sed 1
217.Sh HISTORY
218The
219.Nm grep
220command appeared in
221.At v6 .
222.Sh BUGS
223Lines are limited to 256 characters; longer lines are truncated.
224