1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
.\" $OpenBSD: grep.1,v 1.2 2002/11/09 02:22:33 fgsch Exp $
.\"
.\" Copyright (c) 2000 Carson Harding. All rights reserved.
.\" This code was written and contributed to OpenBSD by Carson Harding.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the author, or the names of contributors may be
.\" used to endorse or promote products derived from this software without
.\" specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd May 10, 2001
.Dt GREP 1
.Os
.Sh NAME
.Nm grep , egrep , fgrep
.Nd print lines matching a pattern
.Sh SYNOPSIS
.Nm grep
.Op Fl E | Fl F
.Op Fl bchilnqsvwxz
.Oo
.Fl RXH
.Op Fl L | Fl P
.Oc
.Op Fl e Ar pattern_list
.Op Fl f Ar pattern_file
.Op Ar pattern
.Op Ar file
.Ar ...
.Sh DESCRIPTION
The
.Nm
utility searches for lines that contain the specified pattern
or patterns. By default
.Nm
prints lines containing matches to the standard output. If no input files
are specified,
.Nm
expects to read its input from the standard input.
.Pp
.Nm egrep
is equivalent to
.Nm grep
.Fl E ;
.Nm fgrep
is equivalent to
.Nm grep
.Fl F .
.Pp
The patterns are regular expressions, or in the case of
.Nm fgrep ,
fixed strings. More than one pattern may be specified on the
command-line, either by repeated use of the
.Fl e
flag, or by separating patterns with newlines:
.Nm grep
\'Ishmael
Ahab' mobydick.txt
is the same as:
.Nm
-e Ishmael -e Ahab mobydick.txt
Note that many regular expression special characters also have special
meaning to the shell. It is best to enclose the regular expression
in quotation marks. Note also that a null pattern ("" or a newline alone)
matches all input.
.Sh OPTIONS
.Pp
The options available are:
.Bl -tag -width file Ds
.It Fl E
Use extended regular expressions, as if the program was invoked as
.Nm egrep.
.It Fl F
Patterns are fixed strings, not regular expressions, as if the
program was invoked as
.Nm fgrep.
.It Fl a
Does nothing. In other greps this flag tells grep to treat the input
as ascii and turns off attempts to determine whether the input file is a
binary file. This
.Nm grep
does not (yet) do that check, so behaves as if
.Fl a
is always specified.
.It Fl b
Prepend the byte-offset of the beginning of the line containing the match.
.It Fl c
Print only a count of the matches found.
.It Fl e Ar pattern_list
The argument to
.Fl e
is a list of patterns to search for.
.Fl e
may be used multiple times, each instance of which may have
one or more patterns separated by new-lines.
.It Fl f Ar pattern_file
The argument to
.Fl f
is the name of a file from which to read patterns.
.Fl f
may be more than once.
.It Fl h
Suppress the prefixing of file names to matching lines when multiple
files are searched.
.It Fl i
Ignore case in comparisons.
.It Fl l
Print only a list of the names of files in which matches were found.
If the input is the standard input,
.Nm
prints "(standard input)".
.It Fl n
Prefix matching lines with their line number in the file in which
they occur.
.It Fl q
Be quiet about errors accessing files, only return program status.
The status returned is 0 if there was a match, 1 if there were no
matches, even if there were errors accessing files. (Without the
.Fl q
flag, if there was an error
accessing a file
.Nm
would return a status of 2 even
if it found matches.)
.It Fl s
Suppress errors about accessing files. Note that unlike
.Fl q
the program is not quiet: matches are still output.
.It Fl v
Invert the sense of the match: return all lines that do not
contain the pattern.
.It Fl w
The pattern is considered a word. A matching pattern in a line must not
be immediately bordered by a letter, a number, or an underscore ('_').
.It Fl x
Match the line exactly. If a regular expression (invoked as
.Nm grep ,
or
.Nm egrep )
the pattern must match the whole line (as if
the pattern was enclosed in ^ and $). If invoked as
.Nm fgrep,
the string must match the entire line.
.It Fl z
When the
.Fl l
flag is also specified, print an ASCII NUL character (0) following
the file name, rather than a newline. Like the
.Fl print0
option to
.Nm find ,
this may be used to pass file names containing unusual characters
to programs such as
.Nm xargs
with the
.Fl 0
flag.
.El
.Sh FILESYSTEM TRAVERSAL OPTIONS
.Pp
The following options control recursive searchs:
.Bl -tag -width file Ds
.It Fl R
Recursively descend through any specified directory arguments.
.It Fl H
If the
.Fl R
option is also specified, symbolic links on the command
line are followed. (Symbolic links encountered in the tree
traversal are not followed.)
.It Fl L
If the
.Fl R
option is also specified, all symbolic links are followed.
.It Fl P
If the
.Fl R
option is also specified, no symbolic links are followed.
.It Fl X
If the
.Fl R
option is also specified, searches are confined to the device on
which the search started (file system mount points are not crossed).
.El
.Sh EXAMPLES
.Pp
To print all occurrences of the word Ishmael in mobydick.txt:
.Dl grep Ishmael mobydick.txt
.Pp
To merely count all the lines in which the word Ishmael occurs:
.Dl grep -c Ishmael mobydick.txt
.Pp
To print all occurrences of either of the words Ishmael or Ahab in
mobydick.txt:
.Dl grep 'Ishmael|Ahab' mobydick.txt
.Pp
To print all occurrences of the word whale in mobydick.txt, whether
or not it is capitalised, and where it appears alone and not as part
of a compound:
.Dl grep -iw whale mobydick.txt
.Pp
To find all the empty lines and print the line numbers for where they occur:
.Dl grep -nv \&. mobydick.txt
.Sh DIAGNOSTICS
.Pp
The
.Nm
utility exits with one of the following values:
.Pp
.Bl -tag -width flag -compact
.It 0
One or more matching lines was found.
.It 1
No matches were found.
.It 2
An error occurred (whether or not matches were found).
.Pp
Note that when the
.Fl q
flag is used, file access errors do not cause an exit value of 2,
and in the absence of other errors (a bad regular expression)
the exit value is determined only by whether or not matches
were found. (XX Should -s do the same??)
.Sh SEE ALSO
.Xr regex 3 ,
.Xr re_format 7
.Sh HISTORY
A
.Nm grep
command appeared in
.At v6 .
This version is a re-implementation from the POSIX specification and
inspection of the operation of several implementations of
.Nm grep .
.Sh NOTES
.Nm grep
has no limits on input line length (other than imposed by available
memory).
|