1GREP(1P) POSIX Programmer's Manual GREP(1P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 grep - search a file for a pattern
13
15 grep [-E| -F][-c| -l| -q][-insvx] -e pattern_list...
16 [-f pattern_file]...[file...]
17
18 grep [-E| -F][-c| -l| -q][-insvx][-e pattern_list]...
19 -f pattern_file...[file...]
20
21 grep [-E| -F][-c| -l| -q][-insvx] pattern_list[file...]
22
23
25 The grep utility shall search the input files, selecting lines matching
26 one or more patterns; the types of patterns are controlled by the
27 options specified. The patterns are specified by the -e option, -f
28 option, or the pattern_list operand. The pattern_list's value shall
29 consist of one or more patterns separated by <newline>s; the pat‐
30 tern_file's contents shall consist of one or more patterns terminated
31 by <newline>. By default, an input line shall be selected if any pat‐
32 tern, treated as an entire basic regular expression (BRE) as described
33 in the Base Definitions volume of IEEE Std 1003.1-2001, Section 9.3,
34 Basic Regular Expressions, matches any part of the line excluding the
35 terminating <newline>; a null BRE shall match every line. By default,
36 each selected input line shall be written to the standard output.
37
38 Regular expression matching shall be based on text lines. Since a <new‐
39 line> separates or terminates patterns (see the -e and -f options
40 below), regular expressions cannot contain a <newline>. Similarly,
41 since patterns are matched against individual lines (excluding the ter‐
42 minating <newline>s) of the input, there is no way for a pattern to
43 match a <newline> found in the input.
44
46 The grep utility shall conform to the Base Definitions volume of
47 IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
48
49 The following options shall be supported:
50
51 -E Match using extended regular expressions. Treat each pattern
52 specified as an ERE, as described in the Base Definitions volume
53 of IEEE Std 1003.1-2001, Section 9.4, Extended Regular Expres‐
54 sions. If any entire ERE pattern matches some part of an input
55 line excluding the terminating <newline>, the line shall be
56 matched. A null ERE shall match every line.
57
58 -F Match using fixed strings. Treat each pattern specified as a
59 string instead of a regular expression. If an input line con‐
60 tains any of the patterns as a contiguous sequence of bytes, the
61 line shall be matched. A null string shall match every line.
62
63 -c Write only a count of selected lines to standard output.
64
65 -e pattern_list
66
67 Specify one or more patterns to be used during the search for
68 input. The application shall ensure that patterns in pat‐
69 tern_list are separated by a <newline>. A null pattern can be
70 specified by two adjacent <newline>s in pattern_list. Unless the
71 -E or -F option is also specified, each pattern shall be treated
72 as a BRE, as described in the Base Definitions volume of
73 IEEE Std 1003.1-2001, Section 9.3, Basic Regular Expressions.
74 Multiple -e and -f options shall be accepted by the grep util‐
75 ity. All of the specified patterns shall be used when matching
76 lines, but the order of evaluation is unspecified.
77
78 -f pattern_file
79
80 Read one or more patterns from the file named by the pathname
81 pattern_file. Patterns in pattern_file shall be terminated by a
82 <newline>. A null pattern can be specified by an empty line in
83 pattern_file. Unless the -E or -F option is also specified, each
84 pattern shall be treated as a BRE, as described in the Base Def‐
85 initions volume of IEEE Std 1003.1-2001, Section 9.3, Basic Reg‐
86 ular Expressions.
87
88 -i Perform pattern matching in searches without regard to case; see
89 the Base Definitions volume of IEEE Std 1003.1-2001, Section
90 9.2, Regular Expression General Requirements.
91
92 -l (The letter ell.) Write only the names of files containing
93 selected lines to standard output. Pathnames shall be written
94 once per file searched. If the standard input is searched, a
95 pathname of "(standard input)" shall be written, in the POSIX
96 locale. In other locales, "standard input" may be replaced by
97 something more appropriate in those locales.
98
99 -n Precede each output line by its relative line number in the
100 file, each file starting at line 1. The line number counter
101 shall be reset for each file processed.
102
103 -q Quiet. Nothing shall be written to the standard output, regard‐
104 less of matching lines. Exit with zero status if an input line
105 is selected.
106
107 -s Suppress the error messages ordinarily written for nonexistent
108 or unreadable files. Other error messages shall not be sup‐
109 pressed.
110
111 -v Select lines not matching any of the specified patterns. If the
112 -v option is not specified, selected lines shall be those that
113 match any of the specified patterns.
114
115 -x Consider only input lines that use all characters in the line
116 excluding the terminating <newline> to match an entire fixed
117 string or regular expression to be matching lines.
118
119
121 The following operands shall be supported:
122
123 pattern_list
124 Specify one or more patterns to be used during the search for
125 input. This operand shall be treated as if it were specified as
126 -e pattern_list.
127
128 file A pathname of a file to be searched for the patterns. If no file
129 operands are specified, the standard input shall be used.
130
131
133 The standard input shall be used only if no file operands are speci‐
134 fied. See the INPUT FILES section.
135
137 The input files shall be text files.
138
140 The following environment variables shall affect the execution of grep:
141
142 LANG Provide a default value for the internationalization variables
143 that are unset or null. (See the Base Definitions volume of
144 IEEE Std 1003.1-2001, Section 8.2, Internationalization Vari‐
145 ables for the precedence of internationalization variables used
146 to determine the values of locale categories.)
147
148 LC_ALL If set to a non-empty string value, override the values of all
149 the other internationalization variables.
150
151 LC_COLLATE
152
153 Determine the locale for the behavior of ranges, equivalence
154 classes, and multi-character collating elements within regular
155 expressions.
156
157 LC_CTYPE
158 Determine the locale for the interpretation of sequences of
159 bytes of text data as characters (for example, single-byte as
160 opposed to multi-byte characters in arguments and input files)
161 and the behavior of character classes within regular expres‐
162 sions.
163
164 LC_MESSAGES
165 Determine the locale that should be used to affect the format
166 and contents of diagnostic messages written to standard error.
167
168 NLSPATH
169 Determine the location of message catalogs for the processing of
170 LC_MESSAGES .
171
172
174 Default.
175
177 If the -l option is in effect, and the -q option is not, the following
178 shall be written for each file containing at least one selected input
179 line:
180
181
182 "%s\n", <file>
183
184 Otherwise, if more than one file argument appears, and -q is not speci‐
185 fied, the grep utility shall prefix each output line by:
186
187
188 "%s:", <file>
189
190 The remainder of each output line shall depend on the other options
191 specified:
192
193 * If the -c option is in effect, the remainder of each output line
194 shall contain:
195
196
197 "%d\n", <count>
198
199 * Otherwise, if -c is not in effect and the -n option is in effect,
200 the following shall be written to standard output:
201
202
203 "%d:", <line number>
204
205 * Finally, the following shall be written to standard output:
206
207
208 "%s", <selected-line contents>
209
211 The standard error shall be used only for diagnostic messages.
212
214 None.
215
217 None.
218
220 The following exit values shall be returned:
221
222 0 One or more lines were selected.
223
224 1 No lines were selected.
225
226 >1 An error occurred.
227
228
230 If the -q option is specified, the exit status shall be zero if an
231 input line is selected, even if an error was detected. Otherwise,
232 default actions shall be performed.
233
234 The following sections are informative.
235
237 Care should be taken when using characters in pattern_list that may
238 also be meaningful to the command interpreter. It is safest to enclose
239 the entire pattern_list argument in single quotes:
240
241
242 '...'
243
244 The -e pattern_list option has the same effect as the pattern_list op‐
245 erand, but is useful when pattern_list begins with the hyphen delim‐
246 iter. It is also useful when it is more convenient to provide multiple
247 patterns as separate arguments.
248
249 Multiple -e and -f options are accepted and grep uses all of the pat‐
250 terns it is given while matching input text lines. (Note that the order
251 of evaluation is not specified. If an implementation finds a null
252 string as a pattern, it is allowed to use that pattern first, matching
253 every line, and effectively ignore any other patterns.)
254
255 The -q option provides a means of easily determining whether or not a
256 pattern (or string) exists in a group of files. When searching several
257 files, it provides a performance improvement (because it can quit as
258 soon as it finds the first match) and requires less care by the user in
259 choosing the set of files to supply as arguments (because it exits zero
260 if it finds a match even if grep detected an access or read error on
261 earlier file operands).
262
264 1. To find all uses of the word "Posix" (in any case) in file text.mm
265 and write with line numbers:
266
267
268 grep -i -n posix text.mm
269
270 2. To find all empty lines in the standard input:
271
272
273 grep ^$
274
275 or:
276
277
278 grep -v .
279
280 3. Both of the following commands print all lines containing strings
281 "abc" or "def" or both:
282
283
284 grep -E 'abc|def'
285
286
287 grep -F 'abc
288 def'
289
290 4. Both of the following commands print all lines matching exactly
291 "abc" or "def" :
292
293
294 grep -E '^abc$|^def$'
295
296
297 grep -F -x 'abc
298 def'
299
301 This grep has been enhanced in an upwards-compatible way to provide the
302 exact functionality of the historical egrep and fgrep commands as well.
303 It was the clear intention of the standard developers to consolidate
304 the three greps into a single command.
305
306 The old egrep and fgrep commands are likely to be supported for many
307 years to come as implementation extensions, allowing historical appli‐
308 cations to operate unmodified.
309
310 Historical implementations usually silently ignored all but one of mul‐
311 tiply-specified -e and -f options, but were not consistent as to which
312 specification was actually used.
313
314 The -b option was omitted from the OPTIONS section because block num‐
315 bers are implementation-defined.
316
317 The System V restriction on using - to mean standard input was omitted.
318
319 A definition of action taken when given a null BRE or ERE is specified.
320 This is an error condition in some historical implementations.
321
322 The -l option previously indicated that its use was undefined when no
323 files were explicitly named. This behavior was historical and placed an
324 unnecessary restriction on future implementations. It has been
325 removed.
326
327 The historical BSD grep -s option practice is easily duplicated by
328 redirecting standard output to /dev/null. The -s option required here
329 is from System V.
330
331 The -x option, historically available only with fgrep, is available
332 here for all of the non-obsolescent versions.
333
335 None.
336
338 sed
339
341 Portions of this text are reprinted and reproduced in electronic form
342 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
343 -- Portable Operating System Interface (POSIX), The Open Group Base
344 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
345 Electrical and Electronics Engineers, Inc and The Open Group. In the
346 event of any discrepancy between this version and the original IEEE and
347 The Open Group Standard, the original IEEE and The Open Group Standard
348 is the referee document. The original Standard can be obtained online
349 at http://www.opengroup.org/unix/online.html .
350
351
352
353IEEE/The Open Group 2003 GREP(1P)