470 lines
17 KiB
HTML
470 lines
17 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
|
|
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
|
|
<title>grep</title>
|
|
</head>
|
|
<body bgcolor="white">
|
|
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
|
|
</script>
|
|
|
|
<basefont size="3"> <a name="grep"></a> <a name="tag_04_63"></a><!-- grep -->
|
|
<!--header start-->
|
|
<center><font size="2">The Open Group Base Specifications Issue 6<br>
|
|
IEEE Std 1003.1-2001<br>
|
|
Copyright © 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
|
|
|
|
<!--header end-->
|
|
<hr size="2" noshade>
|
|
<h4><a name="tag_04_63_01"></a>NAME</h4>
|
|
|
|
<blockquote>grep - search a file for a pattern</blockquote>
|
|
|
|
<h4><a name="tag_04_63_02"></a>SYNOPSIS</h4>
|
|
|
|
<blockquote class="synopsis">
|
|
<p><code><tt>grep</tt> <b>[</b><tt>-E| -F</tt><b>][</b><tt>-c| -l| -q</tt><b>][</b><tt>-insvx</tt><b>]</b> <tt>-e</tt>
|
|
<i>pattern_list...<br>
|
|
</i> <tt> </tt> <i></i><b>[</b><tt>-f</tt>
|
|
<i>pattern_file</i><b>]</b><tt>...</tt><b>[</b><i>file</i><tt>...</tt><b>]</b><tt><br>
|
|
<br>
|
|
grep</tt> <b>[</b><tt>-E| -F</tt><b>][</b><tt>-c| -l| -q</tt><b>][</b><tt>-insvx</tt><b>][</b><tt>-e</tt>
|
|
<i>pattern_list</i><b>]...<br>
|
|
</b> <tt> </tt> <b></b><tt>-f</tt>
|
|
<i>pattern_file</i><tt>...</tt><b>[</b><i>file</i><tt>...</tt><b>]</b><tt><br>
|
|
<br>
|
|
grep</tt> <b>[</b><tt>-E| -F</tt><b>][</b><tt>-c| -l| -q</tt><b>][</b><tt>-insvx</tt><b>]</b>
|
|
<i>pattern_list</i><b>[</b><i>file</i><tt>...</tt><b>]</b><tt><br>
|
|
</tt></code></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_03"></a>DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>grep</i> 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 <b>-e</b> option, <b>-f</b> option, or the
|
|
<i>pattern_list</i> operand. The <i>pattern_list</i>'s value shall consist of one or more patterns separated by <newline>s;
|
|
the <i>pattern_file</i>'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, <a href="../basedefs/xbd_chap09.html#tag_09_03">Section 9.3, Basic Regular Expressions</a>,
|
|
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.</p>
|
|
|
|
<p>Regular expression matching shall be based on text lines. Since a <newline> separates or terminates patterns (see the
|
|
<b>-e</b> and <b>-f</b> 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.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_04"></a>OPTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>grep</i> utility shall conform to the Base Definitions volume of IEEE Std 1003.1-2001, <a href=
|
|
"../basedefs/xbd_chap12.html#tag_12_02">Section 12.2, Utility Syntax Guidelines</a>.</p>
|
|
|
|
<p>The following options shall be supported:</p>
|
|
|
|
<dl compact>
|
|
<dt><b>-E</b></dt>
|
|
|
|
<dd>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, <a href="../basedefs/xbd_chap09.html#tag_09_04">Section 9.4, Extended Regular Expressions</a>.
|
|
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.</dd>
|
|
|
|
<dt><b>-F</b></dt>
|
|
|
|
<dd>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.</dd>
|
|
|
|
<dt><b>-c</b></dt>
|
|
|
|
<dd>Write only a count of selected lines to standard output.</dd>
|
|
|
|
<dt><b>-e </b> <i>pattern_list</i></dt>
|
|
|
|
<dd><br>
|
|
Specify one or more patterns to be used during the search for input. The application shall ensure that patterns in
|
|
<i>pattern_list</i> are separated by a <newline>. A null pattern can be specified by two adjacent <newline>s in
|
|
<i>pattern_list</i>. Unless the <b>-E</b> or <b>-F</b> 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, <a href="../basedefs/xbd_chap09.html#tag_09_03">Section
|
|
9.3, Basic Regular Expressions</a>. Multiple <b>-e</b> and <b>-f</b> options shall be accepted by the <i>grep</i> utility. All of
|
|
the specified patterns shall be used when matching lines, but the order of evaluation is unspecified.</dd>
|
|
|
|
<dt><b>-f </b> <i>pattern_file</i></dt>
|
|
|
|
<dd><br>
|
|
Read one or more patterns from the file named by the pathname <i>pattern_file</i>. Patterns in <i>pattern_file</i> shall be
|
|
terminated by a <newline>. A null pattern can be specified by an empty line in <i>pattern_file</i>. Unless the <b>-E</b> or
|
|
<b>-F</b> 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, <a href="../basedefs/xbd_chap09.html#tag_09_03">Section 9.3, Basic Regular Expressions</a>.</dd>
|
|
|
|
<dt><b>-i</b></dt>
|
|
|
|
<dd>Perform pattern matching in searches without regard to case; see the Base Definitions volume of IEEE Std 1003.1-2001,
|
|
<a href="../basedefs/xbd_chap09.html#tag_09_02">Section 9.2, Regular Expression General Requirements</a>.</dd>
|
|
|
|
<dt><b>-l</b></dt>
|
|
|
|
<dd>(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 <tt>"(standard input)"</tt> shall be written, in the POSIX
|
|
locale. In other locales, <tt>"standard input"</tt> may be replaced by something more appropriate in those locales.</dd>
|
|
|
|
<dt><b>-n</b></dt>
|
|
|
|
<dd>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.</dd>
|
|
|
|
<dt><b>-q</b></dt>
|
|
|
|
<dd>Quiet. Nothing shall be written to the standard output, regardless of matching lines. Exit with zero status if an input line is
|
|
selected.</dd>
|
|
|
|
<dt><b>-s</b></dt>
|
|
|
|
<dd>Suppress the error messages ordinarily written for nonexistent or unreadable files. Other error messages shall not be
|
|
suppressed.</dd>
|
|
|
|
<dt><b>-v</b></dt>
|
|
|
|
<dd>Select lines not matching any of the specified patterns. If the <b>-v</b> option is not specified, selected lines shall be
|
|
those that match any of the specified patterns.</dd>
|
|
|
|
<dt><b>-x</b></dt>
|
|
|
|
<dd>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.</dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_05"></a>OPERANDS</h4>
|
|
|
|
<blockquote>
|
|
<p>The following operands shall be supported:</p>
|
|
|
|
<dl compact>
|
|
<dt><i>pattern_list</i></dt>
|
|
|
|
<dd>Specify one or more patterns to be used during the search for input. This operand shall be treated as if it were specified as
|
|
<b>-e</b> <i>pattern_list</i>.</dd>
|
|
|
|
<dt><i>file</i></dt>
|
|
|
|
<dd>A pathname of a file to be searched for the patterns. If no <i>file</i> operands are specified, the standard input shall be
|
|
used.</dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_06"></a>STDIN</h4>
|
|
|
|
<blockquote>
|
|
<p>The standard input shall be used only if no <i>file</i> operands are specified. See the INPUT FILES section.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_07"></a>INPUT FILES</h4>
|
|
|
|
<blockquote>
|
|
<p>The input files shall be text files.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_08"></a>ENVIRONMENT VARIABLES</h4>
|
|
|
|
<blockquote>
|
|
<p>The following environment variables shall affect the execution of <i>grep</i>:</p>
|
|
|
|
<dl compact>
|
|
<dt><i>LANG</i></dt>
|
|
|
|
<dd>Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of
|
|
IEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap08.html#tag_08_02">Section 8.2, Internationalization Variables</a> for
|
|
the precedence of internationalization variables used to determine the values of locale categories.)</dd>
|
|
|
|
<dt><i>LC_ALL</i></dt>
|
|
|
|
<dd>If set to a non-empty string value, override the values of all the other internationalization variables.</dd>
|
|
|
|
<dt><i>LC_COLLATE</i></dt>
|
|
|
|
<dd><br>
|
|
Determine the locale for the behavior of ranges, equivalence classes, and multi-character collating elements within regular
|
|
expressions.</dd>
|
|
|
|
<dt><i>LC_CTYPE</i></dt>
|
|
|
|
<dd>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.</dd>
|
|
|
|
<dt><i>LC_MESSAGES</i></dt>
|
|
|
|
<dd>Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard
|
|
error.</dd>
|
|
|
|
<dt><i>NLSPATH</i></dt>
|
|
|
|
<dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
|
|
Determine the location of message catalogs for the processing of <i>LC_MESSAGES .</i> <img src="../images/opt-end.gif" alt=
|
|
"[Option End]" border="0"></dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_09"></a>ASYNCHRONOUS EVENTS</h4>
|
|
|
|
<blockquote>
|
|
<p>Default.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_10"></a>STDOUT</h4>
|
|
|
|
<blockquote>
|
|
<p>If the <b>-l</b> option is in effect, and the <b>-q</b> option is not, the following shall be written for each file containing
|
|
at least one selected input line:</p>
|
|
|
|
<pre>
|
|
<tt>"%s\n", <</tt><i>file</i><tt>>
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>Otherwise, if more than one <i>file</i> argument appears, and <b>-q</b> is not specified, the <i>grep</i> utility shall prefix
|
|
each output line by:</p>
|
|
|
|
<pre>
|
|
<tt>"%s:", <</tt><i>file</i><tt>>
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>The remainder of each output line shall depend on the other options specified:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<p>If the <b>-c</b> option is in effect, the remainder of each output line shall contain:</p>
|
|
|
|
<pre>
|
|
<tt>"%d\n", <</tt><i>count</i><tt>>
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
|
|
<li>
|
|
<p>Otherwise, if <b>-c</b> is not in effect and the <b>-n</b> option is in effect, the following shall be written to standard
|
|
output:</p>
|
|
|
|
<pre>
|
|
<tt>"%d:", <</tt><i>line number</i><tt>>
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
|
|
<li>
|
|
<p>Finally, the following shall be written to standard output:</p>
|
|
|
|
<pre>
|
|
<tt>"%s", <</tt><i>selected-line contents</i><tt>>
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
</ul>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_11"></a>STDERR</h4>
|
|
|
|
<blockquote>
|
|
<p>The standard error shall be used only for diagnostic messages.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_12"></a>OUTPUT FILES</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_13"></a>EXTENDED DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_14"></a>EXIT STATUS</h4>
|
|
|
|
<blockquote>
|
|
<p>The following exit values shall be returned:</p>
|
|
|
|
<dl compact>
|
|
<dt> 0</dt>
|
|
|
|
<dd>One or more lines were selected.</dd>
|
|
|
|
<dt> 1</dt>
|
|
|
|
<dd>No lines were selected.</dd>
|
|
|
|
<dt>>1</dt>
|
|
|
|
<dd>An error occurred.</dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_15"></a>CONSEQUENCES OF ERRORS</h4>
|
|
|
|
<blockquote>
|
|
<p>If the <b>-q</b> 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.</p>
|
|
</blockquote>
|
|
|
|
<hr>
|
|
<div class="box"><em>The following sections are informative.</em></div>
|
|
|
|
<h4><a name="tag_04_63_16"></a>APPLICATION USAGE</h4>
|
|
|
|
<blockquote>
|
|
<p>Care should be taken when using characters in <i>pattern_list</i> that may also be meaningful to the command interpreter. It is
|
|
safest to enclose the entire <i>pattern_list</i> argument in single quotes:</p>
|
|
|
|
<pre>
|
|
<tt>'...'
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>The <b>-e</b> <i>pattern_list</i> option has the same effect as the <i>pattern_list</i> operand, but is useful when
|
|
<i>pattern_list</i> begins with the hyphen delimiter. It is also useful when it is more convenient to provide multiple patterns as
|
|
separate arguments.</p>
|
|
|
|
<p>Multiple <b>-e</b> and <b>-f</b> options are accepted and <i>grep</i> 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.)</p>
|
|
|
|
<p>The <b>-q</b> 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 <i>grep</i> detected an access or read error on earlier <i>file</i> operands).</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_17"></a>EXAMPLES</h4>
|
|
|
|
<blockquote>
|
|
<ol>
|
|
<li>
|
|
<p>To find all uses of the word <tt>"Posix"</tt> (in any case) in file <b>text.mm</b> and write with line numbers:</p>
|
|
|
|
<pre>
|
|
<tt>grep -i -n posix text.mm
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
|
|
<li>
|
|
<p>To find all empty lines in the standard input:</p>
|
|
|
|
<pre>
|
|
<tt>grep ^$
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>or:</p>
|
|
|
|
<pre>
|
|
<tt>grep -v .
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
|
|
<li>
|
|
<p>Both of the following commands print all lines containing strings <tt>"abc"</tt> or <tt>"def"</tt> or both:</p>
|
|
|
|
<pre>
|
|
<tt>grep -E 'abc|def'
|
|
<br>
|
|
grep -F 'abc|def'
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
|
|
<li>
|
|
<p>Both of the following commands print all lines matching exactly <tt>"abc"</tt> or <tt>"def"</tt> :</p>
|
|
|
|
<pre>
|
|
<tt>grep -E '^abc$|^def$'
|
|
<br>
|
|
grep -F -x 'abc|def'
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
</ol>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_18"></a>RATIONALE</h4>
|
|
|
|
<blockquote>
|
|
<p>This <i>grep</i> has been enhanced in an upwards-compatible way to provide the exact functionality of the historical
|
|
<i>egrep</i> and <i>fgrep</i> commands as well. It was the clear intention of the standard developers to consolidate the three
|
|
<i>grep</i>s into a single command.</p>
|
|
|
|
<p>The old <i>egrep</i> and <i>fgrep</i> commands are likely to be supported for many years to come as implementation extensions,
|
|
allowing historical applications to operate unmodified.</p>
|
|
|
|
<p>Historical implementations usually silently ignored all but one of multiply-specified <b>-e</b> and <b>-f</b> options, but were
|
|
not consistent as to which specification was actually used.</p>
|
|
|
|
<p>The <b>-b</b> option was omitted from the OPTIONS section because block numbers are implementation-defined.</p>
|
|
|
|
<p>The System V restriction on using <b>-</b> to mean standard input was omitted.</p>
|
|
|
|
<p>A definition of action taken when given a null BRE or ERE is specified. This is an error condition in some historical
|
|
implementations.</p>
|
|
|
|
<p>The <b>-l</b> 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.</p>
|
|
|
|
<p>The historical BSD <i>grep</i> <b>-s</b> option practice is easily duplicated by redirecting standard output to
|
|
<b>/dev/null</b>. The <b>-s</b> option required here is from System V.</p>
|
|
|
|
<p>The <b>-x</b> option, historically available only with <i>fgrep</i>, is available here for all of the non-obsolescent
|
|
versions.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_19"></a>FUTURE DIRECTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_20"></a>SEE ALSO</h4>
|
|
|
|
<blockquote>
|
|
<p><a href="sed.html"><i>sed</i></a></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_21"></a>CHANGE HISTORY</h4>
|
|
|
|
<blockquote>
|
|
<p>First released in Issue 2.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_63_22"></a>Issue 6</h4>
|
|
|
|
<blockquote>
|
|
<p>The Open Group Corrigendum U029/5 is applied, correcting the SYNOPSIS.</p>
|
|
|
|
<p>The normative text is reworded to avoid use of the term "must" for application requirements.</p>
|
|
</blockquote>
|
|
|
|
<div class="box"><em>End of informative text.</em></div>
|
|
|
|
<hr>
|
|
<hr size="2" noshade>
|
|
<center><font size="2"><!--footer start-->
|
|
UNIX ® is a registered Trademark of The Open Group.<br>
|
|
POSIX ® is a registered Trademark of The IEEE.<br>
|
|
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
|
|
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
|
|
]</font></center>
|
|
|
|
<!--footer end-->
|
|
<hr size="2" noshade>
|
|
</body>
|
|
</html>
|
|
|