1GREP(P)                    POSIX Programmer's Manual                   GREP(P)
2
3
4

NAME

6       grep - search a file for a pattern
7

SYNOPSIS

9       grep [-E| -F][-c| -l| -q][-insvx] -e pattern_list...
10               [-f pattern_file]...[file...]
11
12       grep [-E| -F][-c| -l| -q][-insvx][-e pattern_list]...
13               -f pattern_file...[file...]
14
15       grep [-E| -F][-c| -l| -q][-insvx] pattern_list[file...]
16
17

DESCRIPTION

19       The grep utility shall search the input files, selecting lines matching
20       one or more patterns; the types  of  patterns  are  controlled  by  the
21       options  specified.  The  patterns  are  specified by the -e option, -f
22       option, or the pattern_list operand.  The  pattern_list's  value  shall
23       consist  of  one  or  more  patterns  separated by <newline>s; the pat‐
24       tern_file's contents shall consist of one or more  patterns  terminated
25       by  <newline>.  By default, an input line shall be selected if any pat‐
26       tern, treated as an entire basic regular expression (BRE) as  described
27       in  the  Base  Definitions volume of IEEE Std 1003.1-2001, Section 9.3,
28       Basic Regular Expressions, matches any part of the line  excluding  the
29       terminating  <newline>;  a null BRE shall match every line. By default,
30       each selected input line shall be written to the standard output.
31
32       Regular expression matching shall be based on text lines. Since a <new‐
33       line>  separates  or  terminates  patterns  (see  the -e and -f options
34       below), regular expressions  cannot  contain  a  <newline>.  Similarly,
35       since patterns are matched against individual lines (excluding the ter‐
36       minating <newline>s) of the input, there is no way  for  a  pattern  to
37       match a <newline> found in the input.
38

OPTIONS

40       The  grep  utility  shall  conform  to  the  Base Definitions volume of
41       IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
42
43       The following options shall be supported:
44
45       -E     Match using extended regular  expressions.  Treat  each  pattern
46              specified as an ERE, as described in the Base Definitions volume
47              of IEEE Std 1003.1-2001, Section 9.4, Extended  Regular  Expres‐
48              sions.   If any entire ERE pattern matches some part of an input
49              line excluding the terminating  <newline>,  the  line  shall  be
50              matched.  A null ERE shall match every line.
51
52       -F     Match  using  fixed  strings.  Treat each pattern specified as a
53              string instead of a regular expression. If an  input  line  con‐
54              tains any of the patterns as a contiguous sequence of bytes, the
55              line shall be matched. A null string shall match every line.
56
57       -c     Write only a count of selected lines to standard output.
58
59       -e  pattern_list
60
61              Specify one or more patterns to be used during  the  search  for
62              input.   The  application  shall  ensure  that  patterns in pat‐
63              tern_list are separated by a <newline>. A null  pattern  can  be
64              specified by two adjacent <newline>s in pattern_list. Unless the
65              -E or -F option is also specified, each pattern shall be treated
66              as  a  BRE,  as  described  in  the  Base  Definitions volume of
67              IEEE Std 1003.1-2001, Section 9.3,  Basic  Regular  Expressions.
68              Multiple  -e  and -f options shall be accepted by the grep util‐
69              ity. All of the specified patterns shall be used  when  matching
70              lines, but the order of evaluation is unspecified.
71
72       -f  pattern_file
73
74              Read  one  or  more patterns from the file named by the pathname
75              pattern_file.  Patterns in pattern_file shall be terminated by a
76              <newline>.  A  null pattern can be specified by an empty line in
77              pattern_file. Unless the -E or -F option is also specified, each
78              pattern shall be treated as a BRE, as described in the Base Def‐
79              initions volume of IEEE Std 1003.1-2001, Section 9.3, Basic Reg‐
80              ular Expressions.
81
82       -i     Perform pattern matching in searches without regard to case; see
83              the Base Definitions  volume  of  IEEE Std 1003.1-2001,  Section
84              9.2, Regular Expression General Requirements.
85
86       -l     (The  letter  ell.)  Write  only  the  names of files containing
87              selected lines to standard output. Pathnames  shall  be  written
88              once  per  file  searched.  If the standard input is searched, a
89              pathname of "(standard input)" shall be written,  in  the  POSIX
90              locale.  In  other  locales, "standard input" may be replaced by
91              something more appropriate in those locales.
92
93       -n     Precede each output line by its  relative  line  number  in  the
94              file,  each  file  starting  at  line 1. The line number counter
95              shall be reset for each file processed.
96
97       -q     Quiet. Nothing shall be written to the standard output,  regard‐
98              less  of  matching lines. Exit with zero status if an input line
99              is selected.
100
101       -s     Suppress the error messages ordinarily written  for  nonexistent
102              or  unreadable  files.  Other  error  messages shall not be sup‐
103              pressed.
104
105       -v     Select lines not matching any of the specified patterns. If  the
106              -v  option  is not specified, selected lines shall be those that
107              match any of the specified patterns.
108
109       -x     Consider only input lines that use all characters  in  the  line
110              excluding  the  terminating  <newline>  to match an entire fixed
111              string or regular expression to be matching lines.
112
113

OPERANDS

115       The following operands shall be supported:
116
117       pattern_list
118              Specify one or more patterns to be used during  the  search  for
119              input.  This operand shall be treated as if it were specified as
120              -e pattern_list.
121
122       file   A pathname of a file to be searched for the patterns. If no file
123              operands are specified, the standard input shall be used.
124
125

STDIN

127       The  standard  input  shall be used only if no file operands are speci‐
128       fied. See the INPUT FILES section.
129

INPUT FILES

131       The input files shall be text files.
132

ENVIRONMENT VARIABLES

134       The following environment variables shall affect the execution of grep:
135
136       LANG   Provide a default value for the  internationalization  variables
137              that  are  unset  or  null.  (See the Base Definitions volume of
138              IEEE Std 1003.1-2001, Section  8.2,  Internationalization  Vari‐
139              ables  for the precedence of internationalization variables used
140              to determine the values of locale categories.)
141
142       LC_ALL If set to a non-empty string value, override the values  of  all
143              the other internationalization variables.
144
145       LC_COLLATE
146
147              Determine  the  locale  for  the behavior of ranges, equivalence
148              classes, and multi-character collating elements  within  regular
149              expressions.
150
151       LC_CTYPE
152              Determine  the  locale  for  the  interpretation of sequences of
153              bytes of text data as characters (for  example,  single-byte  as
154              opposed  to  multi-byte characters in arguments and input files)
155              and the behavior of character  classes  within  regular  expres‐
156              sions.
157
158       LC_MESSAGES
159              Determine  the  locale  that should be used to affect the format
160              and contents of diagnostic messages written to standard error.
161
162       NLSPATH
163              Determine the location of message catalogs for the processing of
164              LC_MESSAGES .
165
166

ASYNCHRONOUS EVENTS

168       Default.
169

STDOUT

171       If  the -l option is in effect, and the -q option is not, the following
172       shall be written for each file containing at least one  selected  input
173       line:
174
175
176              "%s\n", <file>
177
178       Otherwise, if more than one file argument appears, and -q is not speci‐
179       fied, the grep utility shall prefix each output line by:
180
181
182              "%s:", <file>
183
184       The remainder of each output line shall depend  on  the  other  options
185       specified:
186
187        * If  the  -c  option  is in effect, the remainder of each output line
188          shall contain:
189
190
191          "%d\n", <count>
192
193        * Otherwise, if -c is not in effect and the -n option  is  in  effect,
194          the following shall be written to standard output:
195
196
197          "%d:", <line number>
198
199        * Finally, the following shall be written to standard output:
200
201
202          "%s", <selected-line contents>
203

STDERR

205       The standard error shall be used only for diagnostic messages.
206

OUTPUT FILES

208       None.
209

EXTENDED DESCRIPTION

211       None.
212

EXIT STATUS

214       The following exit values shall be returned:
215
216        0     One or more lines were selected.
217
218        1     No lines were selected.
219
220       >1     An error occurred.
221
222

CONSEQUENCES OF ERRORS

224       If  the  -q  option  is  specified, the exit status shall be zero if an
225       input line is selected, even if  an  error  was  detected.   Otherwise,
226       default actions shall be performed.
227
228       The following sections are informative.
229

APPLICATION USAGE

231       Care  should  be  taken  when using characters in pattern_list that may
232       also be meaningful to the command interpreter. It is safest to  enclose
233       the entire pattern_list argument in single quotes:
234
235
236              '...'
237
238       The  -e pattern_list option has the same effect as the pattern_list op‐
239       erand, but is useful when pattern_list begins with  the  hyphen  delim‐
240       iter.  It is also useful when it is more convenient to provide multiple
241       patterns as separate arguments.
242
243       Multiple -e and -f options are accepted and grep uses all of  the  pat‐
244       terns it is given while matching input text lines. (Note that the order
245       of evaluation is not specified.  If  an  implementation  finds  a  null
246       string  as a pattern, it is allowed to use that pattern first, matching
247       every line, and effectively ignore any other patterns.)
248
249       The -q option provides a means of easily determining whether or  not  a
250       pattern (or string) exists in a group of files.  When searching several
251       files, it provides a performance improvement (because it  can  quit  as
252       soon as it finds the first match) and requires less care by the user in
253       choosing the set of files to supply as arguments (because it exits zero
254       if  it  finds  a match even if grep detected an access or read error on
255       earlier file operands).
256

EXAMPLES

258        1. To find all uses of the word "Posix" (in any case) in file  text.mm
259           and write with line numbers:
260
261
262           grep -i -n posix text.mm
263
264        2. To find all empty lines in the standard input:
265
266
267           grep ^$
268
269       or:
270
271
272              grep -v .
273
274        3. Both  of  the following commands print all lines containing strings
275           "abc" or "def" or both:
276
277
278           grep -E 'abc|def'
279
280
281           grep -F 'abc
282           def'
283
284        4. Both of the following commands print  all  lines  matching  exactly
285           "abc" or "def" :
286
287
288           grep -E '^abc$|^def$'
289
290
291           grep -F -x 'abc
292           def'
293

RATIONALE

295       This grep has been enhanced in an upwards-compatible way to provide the
296       exact functionality of the historical egrep and fgrep commands as well.
297       It  was  the  clear intention of the standard developers to consolidate
298       the three greps into a single command.
299
300       The old egrep and fgrep commands are likely to be  supported  for  many
301       years  to come as implementation extensions, allowing historical appli‐
302       cations to operate unmodified.
303
304       Historical implementations usually silently ignored all but one of mul‐
305       tiply-specified  -e and -f options, but were not consistent as to which
306       specification was actually used.
307
308       The -b option was omitted from the OPTIONS section because  block  num‐
309       bers are implementation-defined.
310
311       The System V restriction on using - to mean standard input was omitted.
312
313       A definition of action taken when given a null BRE or ERE is specified.
314       This is an error condition in some historical implementations.
315
316       The -l option previously indicated that its use was undefined  when  no
317       files were explicitly named. This behavior was historical and placed an
318       unnecessary  restriction  on  future  implementations.   It  has   been
319       removed.
320
321       The  historical  BSD  grep  -s  option practice is easily duplicated by
322       redirecting standard output to /dev/null. The -s option  required  here
323       is from System V.
324
325       The  -x  option,  historically  available only with fgrep, is available
326       here for all of the non-obsolescent versions.
327

FUTURE DIRECTIONS

329       None.
330

SEE ALSO

332       sed
333
335       Portions of this text are reprinted and reproduced in  electronic  form
336       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
337       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
338       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
339       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
340       event of any discrepancy between this version and the original IEEE and
341       The Open Group Standard, the original IEEE and The Open Group  Standard
342       is  the  referee document. The original Standard can be obtained online
343       at http://www.opengroup.org/unix/online.html .
344
345
346
347IEEE/The Open Group                  2003                              GREP(P)
Impressum