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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
.\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved
.TH "GREP" P 2003 POSIX
.\" grep
.SH NAME
grep \- search a file for a pattern
.SH SYNOPSIS
.LP
\fBgrep\fP \fB[\fP\fB-E| -F\fP\fB][\fP\fB-c| -l| -q\fP\fB][\fP\fB-insvx\fP\fB]\fP
\fB-e\fP
\fIpattern_list...
.br
\fP \fB\ \ \ \ \ \ \fP \fI\fP\fB[\fP\fB-f\fP
\fIpattern_file\fP\fB]\fP\fB...\fP\fB[\fP\fIfile\fP\fB...\fP\fB]\fP\fB
.br
.sp
grep\fP \fB[\fP\fB-E| -F\fP\fB][\fP\fB-c| -l| -q\fP\fB][\fP\fB-insvx\fP\fB][\fP\fB-e\fP
\fIpattern_list\fP\fB]...
.br
\fP \fB\ \ \ \ \ \ \fP \fB\fP\fB-f\fP
\fIpattern_file\fP\fB...\fP\fB[\fP\fIfile\fP\fB...\fP\fB]\fP\fB
.br
.sp
grep\fP \fB[\fP\fB-E| -F\fP\fB][\fP\fB-c| -l| -q\fP\fB][\fP\fB-insvx\fP\fB]\fP
\fIpattern_list\fP\fB[\fP\fIfile\fP\fB...\fP\fB]\fP\fB
.br
\fP
.SH DESCRIPTION
.LP
The \fIgrep\fP utility shall search the input files, selecting lines
matching one or more patterns; the types of patterns are
controlled by the options specified. The patterns are specified by
the \fB-e\fP option, \fB-f\fP option, or the
\fIpattern_list\fP operand. The \fIpattern_list\fP's value shall consist
of one or more patterns separated by <newline>s;
the \fIpattern_file\fP's contents shall consist of one or more patterns
terminated by <newline>. By default, an input line
shall be selected if any pattern, treated as an entire basic regular
expression (BRE) as described in the Base Definitions volume
of IEEE\ Std\ 1003.1-2001, Section 9.3, Basic Regular Expressions,
matches any part of the line excluding the terminating <newline>;
a null BRE shall match every line. By default, each
selected input line shall be written to the standard output.
.LP
Regular expression matching shall be based on text lines. Since a
<newline> separates or terminates patterns (see the
\fB-e\fP and \fB-f\fP options below), regular expressions cannot contain
a <newline>. Similarly, since patterns are matched
against individual lines (excluding the terminating <newline>s) of
the input, there is no way for a pattern to match a
<newline> found in the input.
.SH OPTIONS
.LP
The \fIgrep\fP utility shall conform to the Base Definitions volume
of IEEE\ Std\ 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
.LP
The following options shall be supported:
.TP 7
\fB-E\fP
Match using extended regular expressions. Treat each pattern specified
as an ERE, as described in the Base Definitions volume
of IEEE\ Std\ 1003.1-2001, Section 9.4, Extended Regular Expressions.
If any entire ERE pattern matches some part of an input line excluding
the terminating <newline>, the line shall be matched.
A null ERE shall match every line.
.TP 7
\fB-F\fP
Match using fixed strings. Treat each pattern specified as a string
instead of a regular expression. If an input line contains
any of the patterns as a contiguous sequence of bytes, the line shall
be matched. A null string shall match every line.
.TP 7
\fB-c\fP
Write only a count of selected lines to standard output.
.TP 7
\fB-e\ \fP \fIpattern_list\fP
.sp
Specify one or more patterns to be used during the search for input.
The application shall ensure that patterns in
\fIpattern_list\fP are separated by a <newline>. A null pattern can
be specified by two adjacent <newline>s in
\fIpattern_list\fP. Unless the \fB-E\fP or \fB-F\fP option is also
specified, each pattern shall be treated as a BRE, as
described in the Base Definitions volume of IEEE\ Std\ 1003.1-2001,
Section
9.3, Basic Regular Expressions. Multiple \fB-e\fP and \fB-f\fP options
shall be accepted by the \fIgrep\fP utility. All of
the specified patterns shall be used when matching lines, but the
order of evaluation is unspecified.
.TP 7
\fB-f\ \fP \fIpattern_file\fP
.sp
Read one or more patterns from the file named by the pathname \fIpattern_file\fP.
Patterns in \fIpattern_file\fP shall be
terminated by a <newline>. A null pattern can be specified by an empty
line in \fIpattern_file\fP. Unless the \fB-E\fP or
\fB-F\fP option is also specified, each pattern shall be treated as
a BRE, as described in the Base Definitions volume of
IEEE\ Std\ 1003.1-2001, Section 9.3, Basic Regular Expressions.
.TP 7
\fB-i\fP
Perform pattern matching in searches without regard to case; see the
Base Definitions volume of IEEE\ Std\ 1003.1-2001,
Section 9.2, Regular Expression General Requirements.
.TP 7
\fB-l\fP
(The letter ell.) Write only the names of files containing selected
lines to standard output. Pathnames shall be written once
per file searched. If the standard input is searched, a pathname of
\fB"(standard input)"\fP shall be written, in the POSIX
locale. In other locales, \fB"standard input"\fP may be replaced by
something more appropriate in those locales.
.TP 7
\fB-n\fP
Precede each output line by its relative line number in the file,
each file starting at line 1. The line number counter shall
be reset for each file processed.
.TP 7
\fB-q\fP
Quiet. Nothing shall be written to the standard output, regardless
of matching lines. Exit with zero status if an input line is
selected.
.TP 7
\fB-s\fP
Suppress the error messages ordinarily written for nonexistent or
unreadable files. Other error messages shall not be
suppressed.
.TP 7
\fB-v\fP
Select lines not matching any of the specified patterns. If the \fB-v\fP
option is not specified, selected lines shall be
those that match any of the specified patterns.
.TP 7
\fB-x\fP
Consider only input lines that use all characters in the line excluding
the terminating <newline> to match an entire
fixed string or regular expression to be matching lines.
.sp
.SH OPERANDS
.LP
The following operands shall be supported:
.TP 7
\fIpattern_list\fP
Specify one or more patterns to be used during the search for input.
This operand shall be treated as if it were specified as
\fB-e\fP \fIpattern_list\fP.
.TP 7
\fIfile\fP
A pathname of a file to be searched for the patterns. If no \fIfile\fP
operands are specified, the standard input shall be
used.
.sp
.SH STDIN
.LP
The standard input shall be used only if no \fIfile\fP operands are
specified. See the INPUT FILES section.
.SH INPUT FILES
.LP
The input files shall be text files.
.SH ENVIRONMENT VARIABLES
.LP
The following environment variables shall affect the execution of
\fIgrep\fP:
.TP 7
\fILANG\fP
Provide a default value for the internationalization variables that
are unset or null. (See the Base Definitions volume of
IEEE\ Std\ 1003.1-2001, Section 8.2, Internationalization Variables
for
the precedence of internationalization variables used to determine
the values of locale categories.)
.TP 7
\fILC_ALL\fP
If set to a non-empty string value, override the values of all the
other internationalization variables.
.TP 7
\fILC_COLLATE\fP
.sp
Determine the locale for the behavior of ranges, equivalence classes,
and multi-character collating elements within regular
expressions.
.TP 7
\fILC_CTYPE\fP
Determine the locale for the interpretation of sequences of bytes
of text data as characters (for example, single-byte as
opposed to multi-byte characters in arguments and input files) and
the behavior of character classes within regular
expressions.
.TP 7
\fILC_MESSAGES\fP
Determine the locale that should be used to affect the format and
contents of diagnostic messages written to standard
error.
.TP 7
\fINLSPATH\fP
Determine the location of message catalogs for the processing of \fILC_MESSAGES
\&.\fP
.sp
.SH ASYNCHRONOUS EVENTS
.LP
Default.
.SH STDOUT
.LP
If the \fB-l\fP option is in effect, and the \fB-q\fP option is not,
the following shall be written for each file containing
at least one selected input line:
.sp
.RS
.nf
\fB"%s\\n", <\fP\fIfile\fP\fB>
\fP
.fi
.RE
.LP
Otherwise, if more than one \fIfile\fP argument appears, and \fB-q\fP
is not specified, the \fIgrep\fP utility shall prefix
each output line by:
.sp
.RS
.nf
\fB"%s:", <\fP\fIfile\fP\fB>
\fP
.fi
.RE
.LP
The remainder of each output line shall depend on the other options
specified:
.IP " *" 3
If the \fB-c\fP option is in effect, the remainder of each output
line shall contain:
.sp
.RS
.nf
\fB"%d\\n", <\fP\fIcount\fP\fB>
\fP
.fi
.RE
.LP
.IP " *" 3
Otherwise, if \fB-c\fP is not in effect and the \fB-n\fP option is
in effect, the following shall be written to standard
output:
.sp
.RS
.nf
\fB"%d:", <\fP\fIline number\fP\fB>
\fP
.fi
.RE
.LP
.IP " *" 3
Finally, the following shall be written to standard output:
.sp
.RS
.nf
\fB"%s", <\fP\fIselected-line contents\fP\fB>
\fP
.fi
.RE
.LP
.SH STDERR
.LP
The standard error shall be used only for diagnostic messages.
.SH OUTPUT FILES
.LP
None.
.SH EXTENDED DESCRIPTION
.LP
None.
.SH EXIT STATUS
.LP
The following exit values shall be returned:
.TP 7
\ 0
One or more lines were selected.
.TP 7
\ 1
No lines were selected.
.TP 7
>1
An error occurred.
.sp
.SH CONSEQUENCES OF ERRORS
.LP
If the \fB-q\fP option is specified, the exit status shall be zero
if an input line is selected, even if an error was detected.
Otherwise, default actions shall be performed.
.LP
\fIThe following sections are informative.\fP
.SH APPLICATION USAGE
.LP
Care should be taken when using characters in \fIpattern_list\fP that
may also be meaningful to the command interpreter. It is
safest to enclose the entire \fIpattern_list\fP argument in single
quotes:
.sp
.RS
.nf
\fB'...'
\fP
.fi
.RE
.LP
The \fB-e\fP \fIpattern_list\fP option has the same effect as the
\fIpattern_list\fP operand, but is useful when
\fIpattern_list\fP begins with the hyphen delimiter. It is also useful
when it is more convenient to provide multiple patterns as
separate arguments.
.LP
Multiple \fB-e\fP and \fB-f\fP options are accepted and \fIgrep\fP
uses all of the patterns it is given while matching input
text lines. (Note that the order of evaluation is not specified. If
an implementation finds a null string as a pattern, it is
allowed to use that pattern first, matching every line, and effectively
ignore any other patterns.)
.LP
The \fB-q\fP option provides a means of easily determining whether
or not a pattern (or string) exists in a group of files.
When searching several files, it provides a performance improvement
(because it can quit as soon as it finds the first match) and
requires less care by the user in choosing the set of files to supply
as arguments (because it exits zero if it finds a match even
if \fIgrep\fP detected an access or read error on earlier \fIfile\fP
operands).
.SH EXAMPLES
.IP " 1." 4
To find all uses of the word \fB"Posix"\fP (in any case) in file \fBtext.mm\fP
and write with line numbers:
.sp
.RS
.nf
\fBgrep -i -n posix text.mm
\fP
.fi
.RE
.LP
.IP " 2." 4
To find all empty lines in the standard input:
.sp
.RS
.nf
\fBgrep ^$
\fP
.fi
.RE
.LP
or:
.sp
.RS
.nf
\fBgrep -v .
\fP
.fi
.RE
.LP
.IP " 3." 4
Both of the following commands print all lines containing strings
\fB"abc"\fP or \fB"def"\fP or both:
.sp
.RS
.nf
\fBgrep -E 'abc|def'
.sp
grep -F 'abc
def'
\fP
.fi
.RE
.LP
.IP " 4." 4
Both of the following commands print all lines matching exactly \fB"abc"\fP
or \fB"def"\fP :
.sp
.RS
.nf
\fBgrep -E '^abc$|^def$'
.sp
grep -F -x 'abc
def'
\fP
.fi
.RE
.LP
.SH RATIONALE
.LP
This \fIgrep\fP has been enhanced in an upwards-compatible way to
provide the exact functionality of the historical
\fIegrep\fP and \fIfgrep\fP commands as well. It was the clear intention
of the standard developers to consolidate the three
\fIgrep\fPs into a single command.
.LP
The old \fIegrep\fP and \fIfgrep\fP commands are likely to be supported
for many years to come as implementation extensions,
allowing historical applications to operate unmodified.
.LP
Historical implementations usually silently ignored all but one of
multiply-specified \fB-e\fP and \fB-f\fP options, but were
not consistent as to which specification was actually used.
.LP
The \fB-b\fP option was omitted from the OPTIONS section because block
numbers are implementation-defined.
.LP
The System V restriction on using \fB-\fP to mean standard input was
omitted.
.LP
A definition of action taken when given a null BRE or ERE is specified.
This is an error condition in some historical
implementations.
.LP
The \fB-l\fP option previously indicated that its use was undefined
when no files were explicitly named. This behavior was
historical and placed an unnecessary restriction on future implementations.
It has been removed.
.LP
The historical BSD \fIgrep\fP \fB-s\fP option practice is easily duplicated
by redirecting standard output to
\fB/dev/null\fP. The \fB-s\fP option required here is from System
V.
.LP
The \fB-x\fP option, historically available only with \fIfgrep\fP,
is available here for all of the non-obsolescent
versions.
.SH FUTURE DIRECTIONS
.LP
None.
.SH SEE ALSO
.LP
\fIsed\fP
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group. In the
event of any discrepancy between this version 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 .
|