CVSROOT: /webcvs/grep Module name: grep Changes by: Jim Meyering <meyering> 20/01/02 18:18:45
Index: html_node/Usage.html =================================================================== RCS file: /webcvs/grep/grep/manual/html_node/Usage.html,v retrieving revision 1.29 retrieving revision 1.30 diff -u -b -r1.29 -r1.30 --- html_node/Usage.html 30 Dec 2018 06:24:22 -0000 1.29 +++ html_node/Usage.html 2 Jan 2020 23:18:44 -0000 1.30 @@ -2,7 +2,7 @@ <html> <!-- This manual is for grep, a pattern matching engine. -Copyright (C) 1999-2002, 2005, 2008-2018 Free Software Foundation, +Copyright (C) 1999-2002, 2005, 2008-2020 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document @@ -14,10 +14,10 @@ <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> -<title>Usage (GNU Grep 3.3)</title> +<title>Usage (GNU Grep 3.4)</title> -<meta name="description" content="Usage (GNU Grep 3.3)"> -<meta name="keywords" content="Usage (GNU Grep 3.3)"> +<meta name="description" content="Usage (GNU Grep 3.4)"> +<meta name="keywords" content="Usage (GNU Grep 3.4)"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="makeinfo"> @@ -83,7 +83,27 @@ The <samp>-i</samp> option causes <code>grep</code> to ignore case, causing it to match the line ‘<samp>Hello, world!</samp>’, which it would not otherwise match. -See <a href="Invoking.html#Invoking">Invoking</a>, for more details about +</p> +<p>Here is a more complex example session, +showing the location and contents of any line +containing ‘<samp>f</samp>’ and ending in ‘<samp>.c</samp>’, +within all files in the current directory whose names +contain ‘<samp>g</samp>’ and end in ‘<samp>.h</samp>’. +The <samp>-n</samp> option outputs line numbers, the <samp>--</samp> argument +treats any later arguments starting with ‘<samp>-</samp>’ as file names not +options, and the empty file <samp>/dev/null</samp> causes file names to be output +even if only one file name happens to be of the form ‘<samp>*g*.h</samp>’. +</p> +<div class="example"> +<pre class="example">$ <kbd>grep -n -- 'f.*\.c$' *g*.h /dev/null</kbd> +argmatch.h:1:/* definitions and prototypes for argmatch.c +</pre></div> + +<p>The only line that contains a match is line 1 of <samp>argmatch.h</samp>. +Note that the regular expression syntax used in the pattern differs +from the globbing syntax that the shell uses to match file names. +</p> +<p>See <a href="Invoking.html#Invoking">Invoking</a>, for more details about how to invoke <code>grep</code>. </p> <a name="index-using-grep_002c-Q_0026A"></a> @@ -94,10 +114,10 @@ <li> How can I list just the names of matching files? <div class="example"> -<pre class="example">grep -l 'main' *.c +<pre class="example">grep -l 'main' test-*.c </pre></div> -<p>lists the names of all C files in the current directory whose contents +<p>lists names of ‘<samp>test-*.c</samp>’ files in the current directory whose contents mention ‘<samp>main</samp>’. </p> </li><li> How do I search directories recursively? @@ -109,42 +129,51 @@ <p>searches for ‘<samp>hello</samp>’ in all files under the <samp>/home/gigi</samp> directory. For more control over which files are searched, -use <code>find</code>, <code>grep</code>, and <code>xargs</code>. +use <code>find</code> and <code>grep</code>. For example, the following command searches only C files: </p> <div class="example"> -<pre class="example">find /home/gigi -name '*.c' -print0 | xargs -0r grep -H 'hello' +<pre class="example">find /home/gigi -name '*.c' ! -type d \ + -exec grep -H 'hello' '{}' + </pre></div> <p>This differs from the command: </p> <div class="example"> -<pre class="example">grep -H 'hello' *.c +<pre class="example">grep -H 'hello' /home/gigi/*.c </pre></div> -<p>which merely looks for ‘<samp>hello</samp>’ in all files in the current -directory whose names end in ‘<samp>.c</samp>’. -The ‘<samp>find ...</samp>’ command line above is more similar to the command: +<p>which merely looks for ‘<samp>hello</samp>’ in non-hidden C files in +<samp>/home/gigi</samp> whose names end in ‘<samp>.c</samp>’. +The <code>find</code> command line above is more similar to the command: </p> <div class="example"> -<pre class="example">grep -rH --include='*.c' 'hello' /home/gigi +<pre class="example">grep -r --include='*.c' 'hello' /home/gigi </pre></div> -</li><li> What if a pattern has a leading ‘<samp>-</samp>’? +</li><li> What if a pattern or file has a leading ‘<samp>-</samp>’? <div class="example"> -<pre class="example">grep -e '--cut here--' * +<pre class="example">grep -- '--cut here--' * </pre></div> <p>searches for all lines matching ‘<samp>--cut here--</samp>’. -Without <samp>-e</samp>, +Without <samp>--</samp>, <code>grep</code> would attempt to parse ‘<samp>--cut here--</samp>’ as a list of -options. +options, and there would be similar problems with any file names +beginning with ‘<samp>-</samp>’. </p> +<p>Alternatively, you can prevent misinterpretation of leading ‘<samp>-</samp>’ +by using <samp>-e</samp> for patterns and leading ‘<samp>./</samp>’ for files: +</p> +<div class="example"> +<pre class="example">grep -e '--cut here--' ./* +</pre></div> + </li><li> Suppose I want to search for a whole word, not a part of a word? <div class="example"> -<pre class="example">grep -w 'hello' * +<pre class="example">grep -w 'hello' test*.log </pre></div> <p>searches only for instances of ‘<samp>hello</samp>’ that are entire words; @@ -154,7 +183,7 @@ For example: </p> <div class="example"> -<pre class="example">grep 'hello\>' * +<pre class="example">grep 'hello\>' test*.log </pre></div> <p>searches only for words ending in ‘<samp>hello</samp>’, so it matches the word @@ -163,7 +192,7 @@ </li><li> How do I output context around the matching lines? <div class="example"> -<pre class="example">grep -C 2 'hello' * +<pre class="example">grep -C 2 'hello' test*.log </pre></div> <p>prints two lines of context around each matching line. @@ -237,7 +266,7 @@ <p>The <code>grep</code> command searches for lines that contain strings that match a pattern. Every line contains the empty string, so an empty pattern causes <code>grep</code> to find a match on each line. It -is not the only such pattern: ‘<samp>^</samp>’, ‘<samp>$</samp>’, ‘<samp>.*</samp>’, and many +is not the only such pattern: ‘<samp>^</samp>’, ‘<samp>$</samp>’, and many other patterns cause <code>grep</code> to match every line. </p> <p>To match empty lines, use the pattern ‘<samp>^$</samp>’. To match blank @@ -252,30 +281,6 @@ <pre class="example">cat /etc/passwd | grep 'alain' - /etc/motd </pre></div> -</li><li> <a name="index-palindromes"></a> -How to express palindromes in a regular expression? - -<p>It can be done by using back-references; -for example, -a palindrome of 4 characters can be written with a BRE: -</p> -<div class="example"> -<pre class="example">grep -w -e '\(.\)\(.\).\2\1' file -</pre></div> - -<p>It matches the word “radar” or “civic.” -</p> -<p>Guglielmo Bondioni proposed a single RE -that finds all palindromes up to 19 characters long -using 9 subexpressions<!-- /@w --> and 9 <span class="nolinebreak">back-references</span><!-- /@w -->: -</p> -<div class="smallexample"> -<pre class="smallexample">grep -E -e '^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\9\8\7\6\5\4\3\2\1$' file -</pre></div> - -<p>Note this is done by using GNU ERE extensions; -it might not be portable to other implementations of <code>grep</code>. -</p> </li><li> Why is this back-reference failing? <div class="example">
