CVSROOT: /webcvs/grep Module name: grep Changes by: Jim Meyering <meyering> 22/09/03 15:33:15
Index: html_node/Usage.html =================================================================== RCS file: /webcvs/grep/grep/manual/html_node/Usage.html,v retrieving revision 1.32 retrieving revision 1.33 diff -u -b -r1.32 -r1.33 --- html_node/Usage.html 14 Aug 2021 20:46:41 -0000 1.32 +++ html_node/Usage.html 3 Sep 2022 19:33:14 -0000 1.33 @@ -5,7 +5,7 @@ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!-- This manual is for grep, a pattern matching engine. -Copyright (C) 1999-2002, 2005, 2008-2021 Free Software Foundation, +Copyright (C) 1999-2002, 2005, 2008-2022 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document @@ -14,10 +14,10 @@ Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". --> -<title>Usage (GNU Grep 3.7)</title> +<title>Usage (GNU Grep 3.8)</title> -<meta name="description" content="Usage (GNU Grep 3.7)"> -<meta name="keywords" content="Usage (GNU Grep 3.7)"> +<meta name="description" content="Usage (GNU Grep 3.8)"> +<meta name="keywords" content="Usage (GNU Grep 3.8)"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="makeinfo"> @@ -144,24 +144,36 @@ </pre></div> </li><li> What if a pattern or file has a leading ‘<samp>-</samp>’? +For example: <div class="example"> -<pre class="example">grep -- '--cut here--' * +<pre class="example">grep "$pattern" * </pre></div> -<p>searches for all lines matching ‘<samp>--cut here--</samp>’. -Without <samp>--</samp>, -<code>grep</code> would attempt to parse ‘<samp>--cut here--</samp>’ as a list of -options, and there would be similar problems with any file names -beginning with ‘<samp>-</samp>’. +<p>can behave unexpectedly if the value of ‘<samp>pattern</samp>’ begins with ‘<samp>-</samp>’, +or if the ‘<samp>*</samp>’ expands to a file name with leading ‘<samp>-</samp>’. +To avoid the problem, you can use <samp>-e</samp> for patterns and leading +‘<samp>./</samp>’ for files: </p> -<p>Alternatively, you can prevent misinterpretation of leading ‘<samp>-</samp>’ -by using <samp>-e</samp> for patterns and leading ‘<samp>./</samp>’ for files: +<div class="example"> +<pre class="example">grep -e "$pattern" ./* +</pre></div> + +<p>searches for all lines matching the pattern in all the working +directory’s files whose names do not begin with ‘<samp>.</samp>’. +Without the <samp>-e</samp>, <code>grep</code> might treat the pattern as an +option if it begins with ‘<samp>-</samp>’. Without the ‘<samp>./</samp>’, there might +be similar problems with file names beginning with ‘<samp>-</samp>’. +</p> +<p>Alternatively, you can use ‘<samp>--</samp>’ before the pattern and file names: </p> <div class="example"> -<pre class="example">grep -e '--cut here--' ./* +<pre class="example">grep -- "$pattern" * </pre></div> +<p>This also fixes the problem, except that if there is a file named ‘<samp>-</samp>’, +<code>grep</code> misinterprets the ‘<samp>-</samp>’ as standard input. +</p> </li><li> Suppose I want to search for a whole word, not a part of a word? <div class="example"> @@ -235,8 +247,7 @@ <samp>-a</samp> or ‘<samp>--binary-files=text</samp>’ option. To eliminate the “Binary file matches” messages, use the <samp>-I</samp> or -‘<samp>--binary-files=without-match</samp>’ option, -or the <samp>-s</samp> or <samp>--no-messages</samp> option. +‘<samp>--binary-files=without-match</samp>’ option. </p> </li><li> Why doesn’t ‘<samp>grep -lv</samp>’ print non-matching file names? @@ -264,7 +275,10 @@ </p> <p>To match empty lines, use the pattern ‘<samp>^$</samp>’. To match blank lines, use the pattern ‘<samp>^[[:blank:]]*$</samp>’. To match no lines at -all, use the command ‘<samp>grep -f /dev/null</samp>’. +all, use an extended regular expression like ‘<samp>a^</samp>’ or ‘<samp>$a</samp>’. +To match every line, a portable script should use a pattern like +‘<samp>^</samp>’ instead of the empty pattern, as POSIX does not specify the +behavior of the empty pattern. </p> </li><li> How can I search in both standard input and in files? @@ -274,6 +288,21 @@ <pre class="example">cat /etc/passwd | grep 'alain' - /etc/motd </pre></div> +</li><li> Why can’t I combine the shell’s ‘<samp>set -e</samp>’ with <code>grep</code>? + +<p>The <code>grep</code> command follows the convention of programs like +<code>cmp</code> and <code>diff</code> where an exit status of 1 is not an +error. The shell command ‘<samp>set -e</samp>’ causes the shell to exit if +any subcommand exits with nonzero status, and this will cause the +shell to exit merely because <code>grep</code> selected no lines, +which is ordinarily not what you want. +</p> +<p>There is a related problem with Bash’s <code>set -e -o pipefail</code>. +Since <code>grep</code> does not always read all its input, a command +outputting to a pipe read by <code>grep</code> can fail when +<code>grep</code> exits before reading all its input, and the command’s +failure can cause Bash to exit. +</p> </li><li> Why is this back-reference failing? <div class="example"> @@ -304,7 +333,7 @@ <code>sed</code>, <code>perl</code>, or many other utilities that are designed to operate across lines. </p> -</li><li> What do <code>grep</code>, <code>fgrep</code>, and <code>egrep</code> stand for? +</li><li> What do <code>grep</code>, <samp>-E</samp>, and <samp>-F</samp> stand for? <p>The name <code>grep</code> comes from the way line editing was done on Unix. For example, @@ -316,9 +345,29 @@ g/re/p </pre></div> -<p><code>fgrep</code> stands for Fixed <code>grep</code>; -<code>egrep</code> stands for Extended <code>grep</code>. +<p>The <samp>-E</samp> option stands for Extended <code>grep</code>. +The <samp>-F</samp> option stands for Fixed <code>grep</code>; +</p> +</li><li> What happened to <code>egrep</code> and <code>fgrep</code>? + +<p>7th Edition Unix had commands <code>egrep</code> and <code>fgrep</code> +that were the counterparts of the modern ‘<samp>grep -E</samp>’ and ‘<samp>grep -F</samp>’. +Although breaking up <code>grep</code> into three programs was perhaps +useful on the small computers of the 1970s, <code>egrep</code> and +<code>fgrep</code> were not standardized by POSIX and are no longer needed. +In the current GNU implementation, <code>egrep</code> and <code>fgrep</code> +issue a warning and then act like their modern counterparts; +eventually, they are planned to be removed entirely. +</p> +<p>If you prefer the old names, you can use use your own substitutes, +such as a shell script named <code>egrep</code> with the following +contents: </p> +<div class="example"> +<pre class="example">#!/bin/sh +exec grep -E "$@" +</pre></div> + </li></ol>
