650 grep support for -q would be useful
[illumos-gate.git] / usr / src / man / man1 / grep.1
blobe93f3a19ca64fd73fd4c09e0a2941ab97009a8aa
1 '\" te
2 .\" Copyright 2012 Nexenta Systems, Inc. All rights reserved.
3 .\" Copyright 1989 AT&T
4 .\" Copyright (c) 2008, Sun Microsystems, Inc.  All Rights Reserved
5 .\" Portions Copyright (c) 1992, X/Open Company Limited  All Rights Reserved
6 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
7 .\" http://www.opengroup.org/bookstore/.
8 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
9 .\"  This notice shall appear on any product containing this material.
10 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
11 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
12 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
13 .TH GREP 1 "Feb 26, 2008"
14 .SH NAME
15 grep \- search a file for a pattern
16 .SH SYNOPSIS
17 .LP
18 .nf
19 \fB/usr/bin/grep\fR [\fB-c\fR | \fB-l\fR | \fB-q\fR] [\fB-bhinsvw\fR] \fIlimited-regular-expression\fR
20      [\fIfilename\fR]...
21 .fi
23 .LP
24 .nf
25 \fB/usr/xpg4/bin/grep\fR [\fB-E\fR | \fB-F\fR] [\fB-c\fR | \fB-l\fR | \fB-q\fR] [\fB-bhinsvwx\fR] \fB-e\fR \fIpattern_list\fR...
26      [\fB-f\fR \fIpattern_file\fR]... [\fIfile\fR]...
27 .fi
29 .LP
30 .nf
31 \fB/usr/xpg4/bin/grep\fR [\fB-E\fR | \fB-F\fR] [\fB-c\fR | \fB-l\fR | \fB-q\fR] [\fB-bhinsvwx\fR]
32      [\fB-e\fR \fIpattern_list\fR]... \fB-f\fR \fIpattern_file\fR... [\fIfile\fR]...
33 .fi
35 .LP
36 .nf
37 \fB/usr/xpg4/bin/grep\fR [\fB-E\fR | \fB-F\fR] [\fB-c\fR | \fB-l\fR | \fB-q\fR] [\fB-bhinsvwx\fR] \fIpattern\fR
38      [\fIfile\fR]...
39 .fi
41 .SH DESCRIPTION
42 .sp
43 .LP
44 The \fBgrep\fR utility searches text files for a pattern and prints all lines
45 that contain that pattern.  It uses a compact non-deterministic algorithm.
46 .sp
47 .LP
48 Be careful using the characters \fB$\fR, \fB*\fR, \fB[\fR, \fB^\fR, \fB|\fR,
49 \fB(\fR, \fB)\fR, and \fB\e\fR in the \fIpattern_list\fR because they are also
50 meaningful to the shell. It is safest to enclose the entire \fIpattern_list\fR
51 in single quotes \fBa\'\fR\&...\fBa\'\fR\&.
52 .sp
53 .LP
54 If no files are specified, \fBgrep\fR assumes standard input. Normally, each
55 line found is copied to standard output. The file name is printed before each
56 line found if there is more than one input file.
57 .SS "/usr/bin/grep"
58 .sp
59 .LP
60 The \fB/usr/bin/grep\fR utility uses limited regular expressions like those
61 described on the \fBregexp\fR(5) manual page to match the patterns.
62 .SS "/usr/xpg4/bin/grep"
63 .sp
64 .LP
65 The options \fB-E\fR and \fB-F\fR affect the way \fB/usr/xpg4/bin/grep\fR
66 interprets \fIpattern_list\fR. If \fB-E\fR is specified,
67 \fB/usr/xpg4/bin/grep\fR interprets \fIpattern_list\fR as a full regular
68 expression (see \fB-E\fR for description).  If \fB-F\fR is specified,
69 \fBgrep\fR interprets \fIpattern_list\fR as a fixed string. If neither are
70 specified, \fBgrep\fR interprets \fIpattern_list\fR as a basic regular
71 expression as described on \fBregex\fR(5) manual page.
72 .SH OPTIONS
73 .sp
74 .LP
75 The following options are supported for both \fB/usr/bin/grep\fR and
76 \fB/usr/xpg4/bin/grep\fR:
77 .sp
78 .ne 2
79 .na
80 \fB\fB-b\fR\fR
81 .ad
82 .RS 6n
83 Precedes each line by the block number on which it was found. This can be
84 useful in locating block numbers by context (first block is 0).
85 .RE
87 .sp
88 .ne 2
89 .na
90 \fB\fB-c\fR\fR
91 .ad
92 .RS 6n
93 Prints only a count of the lines that contain the pattern.
94 .RE
96 .sp
97 .ne 2
98 .na
99 \fB\fB-h\fR\fR
101 .RS 6n
102 Prevents the name of the file containing the matching line from being prepended
103 to that line.  Used when searching multiple files.
107 .ne 2
109 \fB\fB-i\fR\fR
111 .RS 6n
112 Ignores upper/lower case distinction during comparisons.
116 .ne 2
118 \fB\fB-l\fR\fR
120 .RS 6n
121 Prints only the names of files with matching lines, separated by NEWLINE
122 characters.  Does not repeat the names of files when the pattern is found more
123 than once.
127 .ne 2
129 \fB\fB-n\fR\fR
131 .RS 6n
132 Precedes each line by its line number in the file (first line is 1).
136 .ne 2
138 \fB\fB-q\fR\fR
140 .RS 6n
141 Quiet. Does not write anything to the standard output, regardless of matching
142 lines. Exits with zero status if an input line is selected.
146 .ne 2
148 \fB\fB-s\fR\fR
150 .RS 6n
151 Suppresses error messages about nonexistent or unreadable files.
155 .ne 2
157 \fB\fB-v\fR\fR
159 .RS 6n
160 Prints all lines except those that contain the pattern.
164 .ne 2
166 \fB\fB-w\fR\fR
168 .RS 6n
169 Searches for the expression as a word as if surrounded by \fB\e<\fR and
170 \fB\e>\fR\&.
173 .SS "/usr/xpg4/bin/grep"
176 The following options are supported for \fB/usr/xpg4/bin/grep\fR only:
178 .ne 2
180 \fB\fB-e\fR \fIpattern_list\fR\fR
182 .RS 19n
183 Specifies one or more patterns to be used during the search for input. Patterns
184 in \fIpattern_list\fR must be separated by a NEWLINE character. A null pattern
185 can be specified by two adjacent newline characters in \fIpattern_list\fR.
186 Unless the \fB-E\fR or \fB-F\fR option is also specified, each pattern is
187 treated as a basic regular expression.  Multiple \fB-e\fR and \fB-f\fR options
188 are accepted by \fBgrep\fR. All of the specified patterns are used when
189 matching lines, but the order of evaluation is unspecified.
193 .ne 2
195 \fB\fB-E\fR\fR
197 .RS 19n
198 Matches using full regular expressions. Treats each pattern specified as a full
199 regular expression. If any entire full regular expression pattern matches an
200 input line, the line is matched. A null full regular expression matches every
201 line. Each pattern is interpreted as a full regular expression as described on
202 the \fBregex\fR(5) manual page, except for \fB\e(\fR and \fB\e)\fR, and
203 including:
204 .RS +4
207 A full regular expression followed by \fB+\fR that matches one or more
208 occurrences of the full regular expression.
210 .RS +4
213 A full regular expression followed by \fB?\fR that matches 0 or 1
214 occurrences of the full regular expression.
216 .RS +4
219 Full regular expressions separated by | or by a new-line that match strings
220 that are matched by any of the expressions.
222 .RS +4
225 A full regular expression that is enclosed in parentheses \fB()\fR for
226 grouping.
228 The order of precedence of operators is \fB[\|]\fR, then \fB*\|?\|+\fR, then
229 concatenation, then | and new-line.
233 .ne 2
235 \fB\fB-f\fR \fIpattern_file\fR\fR
237 .RS 19n
238 Reads one or more patterns from the file named by the path name
239 \fIpattern_file\fR. Patterns in \fIpattern_file\fR are terminated by a NEWLINE
240 character. A null pattern can be specified by an empty line in
241 \fIpattern_file\fR. Unless the \fB-E\fR or \fB-F\fR option is also specified,
242 each pattern is treated as a basic regular expression.
246 .ne 2
248 \fB\fB-F\fR\fR
250 .RS 19n
251 Matches using fixed strings. Treats each pattern specified as a string instead
252 of a regular expression. If an input line contains any of the patterns as a
253 contiguous sequence of bytes, the line is matched. A null string matches every
254 line. See \fBfgrep\fR(1) for more information.
258 .ne 2
260 \fB\fB-x\fR\fR
262 .RS 19n
263 Considers only input lines that use all characters in the line to match an
264 entire fixed string or regular expression to be matching lines.
267 .SH OPERANDS
270 The following operands are supported:
272 .ne 2
274 \fB\fIfile\fR\fR
276 .RS 8n
277 A path name of a file to be searched for the patterns. If no \fIfile\fR
278 operands are specified, the standard input is used.
281 .SS "/usr/bin/grep"
283 .ne 2
285 \fB\fIpattern\fR\fR
287 .RS 11n
288 Specifies a pattern to be used during the search for input.
291 .SS "/usr/xpg4/bin/grep"
293 .ne 2
295 \fB\fIpattern\fR\fR
297 .RS 11n
298 Specifies one or more patterns to be used during the search for input. This
299 operand is treated as if it were specified as \fB-e\fR \fIpattern_list\fR.
302 .SH USAGE
305 The \fB-e\fR \fIpattern_list\fR option has the same effect as the
306 \fIpattern_list\fR operand, but is useful when \fIpattern_list\fR begins with
307 the hyphen delimiter. It is also useful when it is more convenient to provide
308 multiple patterns as separate arguments.
311 Multiple \fB-e\fR and \fB-f\fR options are accepted and \fBgrep\fR uses all of
312 the patterns it is given while matching input text lines. Notice that the order
313 of evaluation is not specified. If an implementation finds a null string as a
314 pattern, it is allowed to use that pattern first, matching every line, and
315 effectively ignore any other patterns.
318 The \fB-q\fR option provides a means of easily determining whether or not a
319 pattern (or string) exists in a group of files. When searching several files,
320 it provides a performance improvement (because it can quit as soon as it finds
321 the first match) and requires less care by the user in choosing the set of
322 files to supply as arguments (because it exits zero if it finds a match even if
323 \fBgrep\fR detected an access or read error on earlier file operands).
324 .SS "Large File Behavior"
327 See \fBlargefile\fR(5) for the description of the behavior of \fBgrep\fR when
328 encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
329 .SH EXAMPLES
331 \fBExample 1 \fRFinding All Uses of a Word
334 To find all uses of the word "\fBPosix\fR" (in any case) in the file
335 \fBtext.mm\fR, and write with line numbers:
338 .in +2
340 example% \fB/usr/bin/grep -i -n posix text.mm\fR
342 .in -2
346 \fBExample 2 \fRFinding All Empty Lines
349 To find all empty lines in the standard input:
352 .in +2
354 example% \fB/usr/bin/grep ^$\fR
356 .in -2
364 .in +2
366 example% \fB/usr/bin/grep -v .\fR
368 .in -2
372 \fBExample 3 \fRFinding Lines Containing Strings
375 All of the following commands print all lines containing strings \fBabc\fR or
376 \fBdef\fR or both:
379 .in +2
381 example% \fB/usr/xpg4/bin/grep 'abc
382 def'\fR
383 example% \fB/usr/xpg4/bin/grep -e 'abc
384 def'\fR
385 example% \fB/usr/xpg4/bin/grep -e 'abc' -e 'def'\fR
386 example% \fB/usr/xpg4/bin/grep -E 'abc|def'\fR
387 example% \fB/usr/xpg4/bin/grep -E -e 'abc|def'\fR
388 example% \fB/usr/xpg4/bin/grep -E -e 'abc' -e 'def'\fR
389 example% \fB/usr/xpg4/bin/grep -E 'abc
390 def'\fR
391 example% \fB/usr/xpg4/bin/grep -E -e 'abc
392 def'\fR
393 example% \fB/usr/xpg4/bin/grep -F -e 'abc' -e 'def'\fR
394 example% \fB/usr/xpg4/bin/grep -F 'abc
395 def'\fR
396 example% \fB/usr/xpg4/bin/grep -F -e 'abc
397 def'\fR
399 .in -2
403 \fBExample 4 \fRFinding Lines with Matching Strings
406 Both of the following commands print all lines matching exactly \fBabc\fR or
407 \fBdef\fR:
410 .in +2
412 example% \fB/usr/xpg4/bin/grep -E '^abc$ ^def$'\fR
413 example% \fB/usr/xpg4/bin/grep -F -x 'abc def'\fR
415 .in -2
418 .SH ENVIRONMENT VARIABLES
421 See \fBenviron\fR(5) for descriptions of the following environment variables
422 that affect the execution of \fBgrep\fR: \fBLANG\fR, \fBLC_ALL\fR,
423 \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
424 .SH EXIT STATUS
427 The following exit values are returned:
429 .ne 2
431 \fB\fB0\fR\fR
433 .RS 5n
434 One or more matches were found.
438 .ne 2
440 \fB\fB1\fR\fR
442 .RS 5n
443 No matches were found.
447 .ne 2
449 \fB\fB2\fR\fR
451 .RS 5n
452 Syntax errors or inaccessible files (even if matches were found).
455 .SH ATTRIBUTES
458 See \fBattributes\fR(5) for descriptions of the following attributes:
459 .SS "/usr/bin/grep"
464 box;
465 c | c
466 l | l .
467 ATTRIBUTE TYPE  ATTRIBUTE VALUE
469 CSI     Not Enabled
472 .SS "/usr/xpg4/bin/grep"
477 box;
478 c | c
479 l | l .
480 ATTRIBUTE TYPE  ATTRIBUTE VALUE
482 CSI     Enabled
484 Interface Stability     Committed
486 Standard        See \fBstandards\fR(5).
489 .SH SEE ALSO
492 \fBegrep\fR(1), \fBfgrep\fR(1), \fBsed\fR(1), \fBsh\fR(1), \fBattributes\fR(5),
493 \fBenviron\fR(5), \fBlargefile\fR(5), \fBregex\fR(5), \fBregexp\fR(5),
494 \fBstandards\fR(5)
495 .SH NOTES
496 .SS "/usr/bin/grep"
499 Lines are limited only by the size of the available virtual memory. If there is
500 a line with embedded nulls, \fBgrep\fR only matches up to the first null. If
501 the line matches, the entire line is printed.
502 .SS "/usr/xpg4/bin/grep"
505 The results are unspecified if input files contain lines longer than
506 \fBLINE_MAX\fR bytes or contain binary data. \fBLINE_MAX\fR is defined in
507 \fB/usr/include/limits.h\fR.