Un outil pour savoir si une commande est posix - retour accueil
git clone git://bebou.netlib.re/isposix
Log | Files | Refs | README |
grep.html (21282B)
1 <!-- Copyright 2001-2024 IEEE and The Open Group, All Rights Reserved --> 2 <!DOCTYPE HTML> 3 <html lang="en"> 4 <head> 5 <meta name="generator" content="HTML Tidy for HTML5 for Linux version 5.8.0"> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group rhtm tool v1.2.4 --> 8 <!-- Copyright (c) 2001-2024 The Open Group, All Rights Reserved --> 9 <title>grep</title> 10 </head> 11 <body bgcolor="white"> 12 <div class="NAVHEADER"> 13 <table summary="Header navigation table" class="nav" width="100%" border="0" cellpadding="0" cellspacing="0"> 14 <tr class="nav"> 15 <td class="nav" width="15%" align="left" valign="bottom"><a href="../utilities/gettext.html" accesskey="P"><<< 16 Previous</a></td> 17 <td class="nav" width="70%" align="center" valign="bottom"><a href="contents.html">Home</a></td> 18 <td class="nav" width="15%" align="right" valign="bottom"><a href="../utilities/hash.html" accesskey="N">Next >>></a></td> 19 </tr> 20 </table> 21 <hr align="left" width="100%"></div> 22 <script language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> 23 <center><font size="2">The Open Group Base Specifications Issue 8<br> 24 IEEE Std 1003.1-2024<br> 25 Copyright © 2001-2024 The IEEE and The Open Group</font></center> 26 <hr size="2" noshade> 27 <a name="top" id="top"></a> <a name="grep" id="grep"></a> <a name="tag_20_55" id="tag_20_55"></a><!-- grep --> 28 <h4 class="mansect"><a name="tag_20_55_01" id="tag_20_55_01"></a>NAME</h4> 29 <blockquote>grep — search a file for a pattern</blockquote> 30 <h4 class="mansect"><a name="tag_20_55_02" id="tag_20_55_02"></a>SYNOPSIS</h4> 31 <blockquote class="synopsis"> 32 <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> 33 <i>pattern_list<br></i> <tt> </tt> <b>[</b><tt>-e</tt> <i>pattern_list</i><b>]</b><tt>...</tt> 34 <b>[</b><tt>-f</tt> <i>pattern_file</i><b>]</b><tt>...</tt> <b>[</b><i>file</i><tt>...</tt><b>]</b> <tt><br> 35 <br> 36 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> 37 <i>pattern_list</i><b>]...<br></b> <tt> </tt> <tt>-f</tt> <i>pattern_file</i> 38 <b>[</b><tt>-f</tt> <i>pattern_file</i><b>]</b><tt>...</tt> <b>[</b><i>file</i><tt>...</tt><b>]</b> <tt><br> 39 <br> 40 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> 41 <b>[</b><i>file</i><tt>...</tt><b>]</b> <tt><br></tt></code></p> 42 </blockquote> 43 <h4 class="mansect"><a name="tag_20_55_03" id="tag_20_55_03"></a>DESCRIPTION</h4> 44 <blockquote> 45 <p>The <i>grep</i> utility shall search the input files, selecting lines matching one or more patterns; the types of patterns are 46 controlled by the options specified. The patterns are specified by the <b>-e</b> option, <b>-f</b> option, or the 47 <i>pattern_list</i> operand. The <i>pattern_list</i>'s value shall consist of one or more patterns separated by <newline> 48 characters; the <i>pattern_file</i>'s contents shall consist of one or more patterns terminated by a <newline> character. By 49 default, an input line shall be selected if any pattern, treated as an entire basic regular expression (BRE) as described in XBD 50 <a href="../basedefs/V1_chap09.html#tag_09_03"><i>9.3 Basic Regular Expressions</i></a> , matches any part of the line excluding 51 the terminating <newline>; a null BRE shall match every line. By default, each selected input line shall be written to the 52 standard output.</p> 53 <p>Regular expression matching shall be based on text lines. Since a <newline> separates or terminates patterns (see the 54 <b>-e</b> and <b>-f</b> options below), regular expressions cannot contain a <newline>. Similarly, since patterns are matched 55 against individual lines (excluding the terminating <newline> characters) of the input, there is no way for a pattern to 56 match a <newline> found in the input.</p> 57 </blockquote> 58 <h4 class="mansect"><a name="tag_20_55_04" id="tag_20_55_04"></a>OPTIONS</h4> 59 <blockquote> 60 <p>The <i>grep</i> utility shall conform to XBD <a href="../basedefs/V1_chap12.html#tag_12_02"><i>12.2 Utility Syntax 61 Guidelines</i></a> .</p> 62 <p>The following options shall be supported:</p> 63 <dl compact> 64 <dd></dd> 65 <dt><b>-E</b></dt> 66 <dd>Match using extended regular expressions. Treat each pattern specified as an ERE, as described in XBD <a href= 67 "../basedefs/V1_chap09.html#tag_09_04"><i>9.4 Extended Regular Expressions</i></a> . If any entire ERE pattern matches some part of 68 an input line excluding the terminating <newline>, the line shall be matched. A null ERE shall match every line.</dd> 69 <dt><b>-F</b></dt> 70 <dd>Match using fixed strings. Treat each pattern specified as a string instead of a regular expression. If an input line contains 71 any of the patterns as a contiguous sequence of bytes, the line shall be matched. A null string shall match every line.</dd> 72 <dt><b>-c</b></dt> 73 <dd>Write only a count of selected lines to standard output.</dd> 74 <dt><b>-e </b><i>pattern_list</i></dt> 75 <dd><br> 76 Specify one or more patterns to be used during the search for input. The application shall ensure that patterns in 77 <i>pattern_list</i> are separated by a <newline>. A null pattern can be specified by two adjacent <newline> characters 78 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 79 described in XBD <a href="../basedefs/V1_chap09.html#tag_09_03"><i>9.3 Basic Regular Expressions</i></a> . Multiple <b>-e</b> and 80 <b>-f</b> options shall be accepted by the <i>grep</i> utility. All of the specified patterns shall be used when matching lines, 81 but the order of evaluation is unspecified.</dd> 82 <dt><b>-f </b><i>pattern_file</i></dt> 83 <dd><br> 84 Read one or more patterns from the file named by the pathname <i>pattern_file</i>. Patterns in <i>pattern_file</i> shall be 85 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 86 <b>-F</b> option is also specified, each pattern shall be treated as a BRE, as described in XBD <a href= 87 "../basedefs/V1_chap09.html#tag_09_03"><i>9.3 Basic Regular Expressions</i></a> .</dd> 88 <dt><b>-i</b></dt> 89 <dd>Perform pattern matching in a case-insensitive manner; see XBD <a href="../basedefs/V1_chap09.html#tag_09_02"><i>9.2 Regular 90 Expression General Requirements</i></a> .</dd> 91 <dt><b>-l</b></dt> 92 <dd>(The letter ell.) Write only the names of files containing selected lines to standard output. Pathnames shall be written once 93 per file searched. If the standard input is searched, a pathname of <tt>"(standard input)"</tt> shall be written, in the POSIX 94 locale. In other locales, <tt>"standard input"</tt> may be replaced by something more appropriate in those locales.</dd> 95 <dt><b>-n</b></dt> 96 <dd>Precede each output line by its relative line number in the file, each file starting at line 1. The line number counter shall 97 be reset for each file processed.</dd> 98 <dt><b>-q</b></dt> 99 <dd>Quiet. Nothing shall be written to the standard output, regardless of matching lines. Exit with zero status if an input line is 100 selected.</dd> 101 <dt><b>-s</b></dt> 102 <dd>Suppress the error messages ordinarily written for nonexistent or unreadable files. Other error messages shall not be 103 suppressed.</dd> 104 <dt><b>-v</b></dt> 105 <dd>Select lines not matching any of the specified patterns. If the <b>-v</b> option is not specified, selected lines shall be 106 those that match any of the specified patterns.</dd> 107 <dt><b>-x</b></dt> 108 <dd>Consider only input lines that use all characters in the line excluding the terminating <newline> to match an entire 109 fixed string or regular expression to be matching lines.</dd> 110 </dl> 111 </blockquote> 112 <h4 class="mansect"><a name="tag_20_55_05" id="tag_20_55_05"></a>OPERANDS</h4> 113 <blockquote> 114 <p>The following operands shall be supported:</p> 115 <dl compact> 116 <dd></dd> 117 <dt><i>pattern_list</i></dt> 118 <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 119 <b>-e</b> <i>pattern_list</i>.</dd> 120 <dt><i>file</i></dt> 121 <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 122 used.</dd> 123 </dl> 124 </blockquote> 125 <h4 class="mansect"><a name="tag_20_55_06" id="tag_20_55_06"></a>STDIN</h4> 126 <blockquote> 127 <p>The standard input shall be used if no <i>file</i> operands are specified, and shall be used if a <i>file</i> operand is 128 <tt>'-'</tt> and the implementation treats the <tt>'-'</tt> as meaning standard input. Otherwise, the standard input shall not be 129 used. See the INPUT FILES section.</p> 130 </blockquote> 131 <h4 class="mansect"><a name="tag_20_55_07" id="tag_20_55_07"></a>INPUT FILES</h4> 132 <blockquote> 133 <p>The input files shall be text files.</p> 134 </blockquote> 135 <h4 class="mansect"><a name="tag_20_55_08" id="tag_20_55_08"></a>ENVIRONMENT VARIABLES</h4> 136 <blockquote> 137 <p>The following environment variables shall affect the execution of <i>grep</i>:</p> 138 <dl compact> 139 <dd></dd> 140 <dt><i>LANG</i></dt> 141 <dd>Provide a default value for the internationalization variables that are unset or null. (See XBD <a href= 142 "../basedefs/V1_chap08.html#tag_08_02"><i>8.2 Internationalization Variables</i></a> for the precedence of internationalization 143 variables used to determine the values of locale categories.)</dd> 144 <dt><i>LC_ALL</i></dt> 145 <dd>If set to a non-empty string value, override the values of all the other internationalization variables.</dd> 146 <dt><i>LC_COLLATE</i></dt> 147 <dd><br> 148 Determine the locale for the behavior of ranges, equivalence classes, and multi-character collating elements within regular 149 expressions.</dd> 150 <dt><i>LC_CTYPE</i></dt> 151 <dd>Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as 152 opposed to multi-byte characters in arguments and input files) and the behavior of character classes within regular 153 expressions.</dd> 154 <dt><i>LC_MESSAGES</i></dt> 155 <dd><br> 156 Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.</dd> 157 <dt><i>NLSPATH</i></dt> 158 <dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> 159 Determine the location of messages objects and message catalogs. <img src="../images/opt-end.gif" alt="[Option End]" border= 160 "0"></dd> 161 </dl> 162 </blockquote> 163 <h4 class="mansect"><a name="tag_20_55_09" id="tag_20_55_09"></a>ASYNCHRONOUS EVENTS</h4> 164 <blockquote> 165 <p>Default.</p> 166 </blockquote> 167 <h4 class="mansect"><a name="tag_20_55_10" id="tag_20_55_10"></a>STDOUT</h4> 168 <blockquote> 169 <p>If the <b>-l</b> option is in effect, the following shall be written for each file containing at least one selected input 170 line:</p> 171 <pre> 172 <tt>"%s\n", <</tt><i>file</i><tt>> 173 </tt></pre> 174 <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 175 each output line by:</p> 176 <pre> 177 <tt>"%s:", <</tt><i>file</i><tt>> 178 </tt></pre> 179 <p>The remainder of each output line shall depend on the other options specified:</p> 180 <ul> 181 <li> 182 <p>If the <b>-c</b> option is in effect, the remainder of each output line shall contain:</p> 183 <pre> 184 <tt>"%d\n", <</tt><i>count</i><tt>> 185 </tt></pre></li> 186 <li> 187 <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 188 output:</p> 189 <pre> 190 <tt>"%d:", <</tt><i>line number</i><tt>> 191 </tt></pre></li> 192 <li> 193 <p>Finally, the following shall be written to standard output:</p> 194 <pre> 195 <tt>"%s", <</tt><i>selected-line contents</i><tt>> 196 </tt></pre></li> 197 </ul> 198 </blockquote> 199 <h4 class="mansect"><a name="tag_20_55_11" id="tag_20_55_11"></a>STDERR</h4> 200 <blockquote> 201 <p>The standard error shall be used only for diagnostic messages.</p> 202 </blockquote> 203 <h4 class="mansect"><a name="tag_20_55_12" id="tag_20_55_12"></a>OUTPUT FILES</h4> 204 <blockquote> 205 <p>None.</p> 206 </blockquote> 207 <h4 class="mansect"><a name="tag_20_55_13" id="tag_20_55_13"></a>EXTENDED DESCRIPTION</h4> 208 <blockquote> 209 <p>None.</p> 210 </blockquote> 211 <h4 class="mansect"><a name="tag_20_55_14" id="tag_20_55_14"></a>EXIT STATUS</h4> 212 <blockquote> 213 <p>The following exit values shall be returned:</p> 214 <dl compact> 215 <dd></dd> 216 <dt> 0</dt> 217 <dd>One or more lines were selected and the output specified in STDOUT was successfully written to standard output.</dd> 218 <dt> 1</dt> 219 <dd>No lines were selected.</dd> 220 <dt>>1</dt> 221 <dd>An error occurred.</dd> 222 </dl> 223 </blockquote> 224 <h4 class="mansect"><a name="tag_20_55_15" id="tag_20_55_15"></a>CONSEQUENCES OF ERRORS</h4> 225 <blockquote> 226 <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. 227 Otherwise, default actions shall be performed.</p> 228 </blockquote> 229 <hr> 230 <div class="box"><em>The following sections are informative.</em></div> 231 <h4 class="mansect"><a name="tag_20_55_16" id="tag_20_55_16"></a>APPLICATION USAGE</h4> 232 <blockquote> 233 <p>Care should be taken when using characters in <i>pattern_list</i> that may also be meaningful to the command interpreter. It is 234 safest to enclose the entire <i>pattern_list</i> argument in single-quotes:</p> 235 <pre> 236 <tt>'...' 237 </tt></pre> 238 <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 239 <i>pattern_list</i> begins with the <hyphen-minus> delimiter. It is also useful when it is more convenient to provide 240 multiple patterns as separate arguments.</p> 241 <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 242 text lines. (Note that the order of evaluation is not specified. If an implementation finds a null string as a pattern, it is 243 allowed to use that pattern first, matching every line, and effectively ignore any other patterns.)</p> 244 <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. 245 When searching several files, it provides a performance improvement (because it can quit as soon as it finds the first match) and 246 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 247 if <i>grep</i> detected an access or read error on earlier <i>file</i> operands).</p> 248 <p>When using <i>grep</i> to process pathnames, it is recommended that LC_ALL, or at least LC_CTYPE and LC_COLLATE, are set to 249 POSIX or C in the environment, since pathnames can contain byte sequences that do not form valid characters in some locales, in 250 which case the utility's behavior would be undefined. In the POSIX locale each byte is a valid single-byte character, and therefore 251 this problem is avoided.</p> 252 </blockquote> 253 <h4 class="mansect"><a name="tag_20_55_17" id="tag_20_55_17"></a>EXAMPLES</h4> 254 <blockquote> 255 <ol> 256 <li> 257 <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> 258 <pre> 259 <tt>grep -i -n posix text.mm 260 </tt></pre></li> 261 <li> 262 <p>To find all empty lines in the standard input:</p> 263 <pre> 264 <tt>grep ^$ 265 </tt></pre> 266 <p>or:</p> 267 <pre> 268 <tt>grep -v . 269 </tt></pre></li> 270 <li> 271 <p>Both of the following commands print all lines containing strings <tt>"abc"</tt> or <tt>"def"</tt> or both:</p> 272 <pre> 273 <tt>grep -E 'abc|def' 274 <br> 275 grep -F 'abc 276 def' 277 </tt></pre></li> 278 <li> 279 <p>Both of the following commands print all lines matching exactly <tt>"abc"</tt> or <tt>"def"</tt>:</p> 280 <pre> 281 <tt>grep -E '^abc$|^def$' 282 <br> 283 grep -F -x 'abc 284 def' 285 </tt></pre></li> 286 </ol> 287 </blockquote> 288 <h4 class="mansect"><a name="tag_20_55_18" id="tag_20_55_18"></a>RATIONALE</h4> 289 <blockquote> 290 <p>This <i>grep</i> has been enhanced in an upwards-compatible way to provide the exact functionality of the historical 291 <i>egrep</i> and <i>fgrep</i> commands as well. It was the clear intention of the standard developers to consolidate the three 292 <i>grep</i>s into a single command.</p> 293 <p>The old <i>egrep</i> and <i>fgrep</i> commands are likely to be supported for many years to come as implementation extensions, 294 allowing historical applications to operate unmodified.</p> 295 <p>Historical implementations usually silently ignored all but one of multiply-specified <b>-e</b> and <b>-f</b> options, but were 296 not consistent as to which specification was actually used.</p> 297 <p>The <b>-b</b> option was omitted from the OPTIONS section because block numbers are implementation-defined.</p> 298 <p>The System V restriction on using <b>-</b> to mean standard input was omitted.</p> 299 <p>A definition of action taken when given a null BRE or ERE is specified. This is an error condition in some historical 300 implementations.</p> 301 <p>The <b>-l</b> option previously indicated that its use was undefined when no files were explicitly named. This behavior was 302 historical and placed an unnecessary restriction on future implementations. It has been removed.</p> 303 <p>The historical BSD <i>grep</i> <b>-s</b> option practice is easily duplicated by redirecting standard output to 304 <b>/dev/null</b>. The <b>-s</b> option required here is from System V.</p> 305 <p>The <b>-x</b> option, historically available only with <i>fgrep</i>, is available here for all of the non-obsolescent 306 versions.</p> 307 </blockquote> 308 <h4 class="mansect"><a name="tag_20_55_19" id="tag_20_55_19"></a>FUTURE DIRECTIONS</h4> 309 <blockquote> 310 <p>If this utility is directed to display a pathname that contains any bytes that have the encoded value of a <newline> 311 character when <newline> is a terminator or separator in the output format being used, implementations are encouraged to 312 treat this as an error. A future version of this standard may require implementations to treat this as an error.</p> 313 </blockquote> 314 <h4 class="mansect"><a name="tag_20_55_20" id="tag_20_55_20"></a>SEE ALSO</h4> 315 <blockquote> 316 <p><a href="../utilities/sed.html#"><i>sed</i></a></p> 317 <p>XBD <a href="../basedefs/V1_chap08.html#tag_08"><i>8. Environment Variables</i></a> , <a href= 318 "../basedefs/V1_chap09.html#tag_09"><i>9. Regular Expressions</i></a> , <a href="../basedefs/V1_chap12.html#tag_12_02"><i>12.2 319 Utility Syntax Guidelines</i></a></p> 320 </blockquote> 321 <h4 class="mansect"><a name="tag_20_55_21" id="tag_20_55_21"></a>CHANGE HISTORY</h4> 322 <blockquote> 323 <p>First released in Issue 2.</p> 324 </blockquote> 325 <h4 class="mansect"><a name="tag_20_55_22" id="tag_20_55_22"></a>Issue 6</h4> 326 <blockquote> 327 <p>The Open Group Corrigendum U029/5 is applied, correcting the SYNOPSIS.</p> 328 <p>The normative text is reworded to avoid use of the term "must" for application requirements.</p> 329 <p>IEEE Std 1003.1-2001/Cor 1-2002, item XCU/TC1/D6/28 is applied, correcting the examples using the <i>grep</i> 330 <b>-F</b> option which did not match the normative description of the <b>-F</b> option.</p> 331 </blockquote> 332 <h4 class="mansect"><a name="tag_20_55_23" id="tag_20_55_23"></a>Issue 7</h4> 333 <blockquote> 334 <p>Austin Group Interpretation 1003.1-2001 #092 is applied.</p> 335 <p>SD5-XCU-ERN-97 is applied, updating the SYNOPSIS.</p> 336 <p>SD5-XCU-ERN-98 is applied, updating the STDOUT section.</p> 337 <p>POSIX.1-2008, Technical Corrigendum 2, XCU/TC2-2008/0105 [584] and XCU/TC2-2008/0106 [663] are applied.</p> 338 </blockquote> 339 <h4 class="mansect"><a name="tag_20_55_24" id="tag_20_55_24"></a>Issue 8</h4> 340 <blockquote> 341 <p>Austin Group Defect 251 is applied, encouraging implementations to report an error if a utility is directed to display a 342 pathname that contains any bytes that have the encoded value of a <newline> character when <newline> is a terminator or 343 separator in the output format being used.</p> 344 <p>Austin Group Defect 1031 is applied, changing the description of the <b>-i</b> option.</p> 345 <p>Austin Group Defect 1122 is applied, changing the description of <i>NLSPATH .</i></p> 346 <p>Austin Group Defect 1502 is applied, changing the EXIT STATUS section.</p> 347 </blockquote> 348 <div class="box"><em>End of informative text.</em></div> 349 <hr> 350 <p> </p> 351 <a href="#top"><span class="topOfPage">return to top of page</span></a><br> 352 <hr size="2" noshade> 353 <center><font size="2">UNIX® is a registered Trademark of The Open Group.<br> 354 POSIX™ is a Trademark of The IEEE.<br> 355 Copyright © 2001-2024 The IEEE and The Open Group, All Rights Reserved<br> 356 [ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href= 357 "../functions/contents.html">XSH</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../xrat/contents.html">XRAT</a> 358 ]</font></center> 359 <hr size="2" noshade> 360 <div class="NAVHEADER"> 361 <table summary="Header navigation table" class="nav" width="100%" border="0" cellpadding="0" cellspacing="0"> 362 <tr class="nav"> 363 <td class="nav" width="15%" align="left" valign="bottom"><a href="../utilities/gettext.html" accesskey="P"><<< 364 Previous</a></td> 365 <td class="nav" width="70%" align="center" valign="bottom"><a href="contents.html">Home</a></td> 366 <td class="nav" width="15%" align="right" valign="bottom"><a href="../utilities/hash.html" accesskey="N">Next >>></a></td> 367 </tr> 368 </table> 369 <hr align="left" width="100%"></div> 370 </body> 371 </html>