[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Changes to manual/html_node/Usage.html

From: Karl Berry
Subject: Changes to manual/html_node/Usage.html
Date: Sun, 24 May 2009 18:43:11 +0000

CVSROOT:        /web/grep
Module name:    grep
Changes by:     Karl Berry <karl>       09/05/24 18:43:01

Index: manual/html_node/Usage.html
RCS file: manual/html_node/Usage.html
diff -N manual/html_node/Usage.html
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ manual/html_node/Usage.html 24 May 2009 18:43:00 -0000      1.1
@@ -0,0 +1,234 @@
+<html lang="en">
+<title>Usage - GNU Grep 2.5.4</title>
+<meta http-equiv="Content-Type" content="text/html">
+<meta name="description" content="GNU Grep 2.5.4">
+<meta name="generator" content="makeinfo 4.13">
+<link title="Top" rel="start" href="index.html#Top">
+<link rel="prev" href="Regular-Expressions.html#Regular-Expressions" 
title="Regular Expressions">
+<link rel="next" href="Reporting-Bugs.html#Reporting-Bugs" title="Reporting 
+<link href=""; rel="generator-home" 
title="Texinfo Homepage">
+This manual is for `grep', a pattern matching engine.
+Copyright (C) 1999, 2000, 2001, 2002, 2005, 2008, 2009 Free
+Software Foundation, Inc.
+     Permission is granted to copy, distribute and/or modify this
+     document under the terms of the GNU Free Documentation License,
+     Version 1.3 or any later version published by the Free Software
+     Foundation; with no 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''.
+   -->
+<meta http-equiv="Content-Style-Type" content="text/css">
+<style type="text/css"><!--
+  pre.display { font-family:inherit }
+  pre.format  { font-family:inherit }
+  pre.smalldisplay { font-family:inherit; font-size:smaller }
+  pre.smallformat  { font-family:inherit; font-size:smaller }
+  pre.smallexample { font-size:smaller }
+  pre.smalllisp    { font-size:smaller }
+    { font-variant:small-caps }
+  span.roman { font-family:serif; font-weight:normal; } 
+  span.sansserif { font-family:sans-serif; font-weight:normal; } 
+<div class="node">
+<a name="Usage"></a>
+Next:&nbsp;<a rel="next" accesskey="n" 
href="Reporting-Bugs.html#Reporting-Bugs">Reporting Bugs</a>,
+Previous:&nbsp;<a rel="previous" accesskey="p" 
href="Regular-Expressions.html#Regular-Expressions">Regular Expressions</a>,
+Up:&nbsp;<a rel="up" accesskey="u" href="index.html#Top">Top</a>
+<h2 class="chapter">4 Usage</h2>
+<p><a name="index-usage_002c-examples-241"></a>Here is an example command that 
invokes <span class="sc">gnu</span> <samp><span 
+<pre class="example">     grep -i 'hello.*world' menu.h main.c
+   <p class="noindent">This lists all lines in the files <samp><span 
class="file">menu.h</span></samp> and <samp><span 
class="file">main.c</span></samp> that
+contain the string &lsquo;<samp><span class="samp">hello</span></samp>&rsquo; 
followed by the string &lsquo;<samp><span 
+this is because &lsquo;<samp><span class="samp">.*</span></samp>&rsquo; 
matches zero or more characters within a line. 
+See <a href="Regular-Expressions.html#Regular-Expressions">Regular 
+The &lsquo;<samp><span class="samp">-i</span></samp>&rsquo; option causes 
<samp><span class="command">grep</span></samp>
+to ignore case, causing it to match the line &lsquo;<samp><span 
class="samp">Hello, world!</span></samp>&rsquo;, which
+it would not otherwise match. 
+See <a href="Invoking.html#Invoking">Invoking</a>, for more details about
+how to invoke <samp><span class="command">grep</span></samp>.
+   <p><a name="index-using-_0040command_007bgrep_007d_002c-Q_0026A-242"></a><a 
name="index-FAQ-about-_0040command_007bgrep_007d-usage-243"></a>Here are some 
common questions and answers about <samp><span 
class="command">grep</span></samp> usage.
+     <ol type=1 start=1>
+     <li>How can I list just the names of matching files?
+     <pre class="example">          grep -l 'main' *.c
+     <p class="noindent">lists the names of all C files in the current 
directory whose contents
+mention &lsquo;<samp><span class="samp">main</span></samp>&rsquo;.
+     <li>How do I search directories recursively?
+     <pre class="example">          grep -r 'hello' /home/gigi
+     <p class="noindent">searches for &lsquo;<samp><span 
class="samp">hello</span></samp>&rsquo; in all files
+under the <samp><span class="file">/home/gigi</span></samp> directory. 
+For more control over which files are searched,
+use <samp><span class="command">find</span></samp>, <samp><span 
class="command">grep</span></samp>, and <samp><span 
+For example, the following command searches only C files:
+     <pre class="example">          find /home/gigi -name '*.c' -print0 | 
xargs -0r grep -H 'hello'
+     <p>This differs from the command:
+     <pre class="example">          grep -rH 'hello' *.c
+     <p>which merely looks for &lsquo;<samp><span 
class="samp">hello</span></samp>&rsquo; in all files in the current
+directory whose names end in &lsquo;<samp><span 
+Here the <samp><span class="option">-r</span></samp> is
+probably unnecessary, as recursion occurs only in the unlikely event
+that one of &lsquo;<samp><span class="samp">.c</span></samp>&rsquo; files is a 
+The &lsquo;<samp><span class="samp">find ...</span></samp>&rsquo; command line 
above is more similar to the command:
+     <pre class="example">          grep -rH --include='*.c' 'hello' /home/gigi
+     <li>What if a pattern has a leading &lsquo;<samp><span 
+     <pre class="example">          grep -e '--cut here--' *
+     <p class="noindent">searches for all lines matching &lsquo;<samp><span 
class="samp">--cut here--</span></samp>&rsquo;. 
+Without &lsquo;<samp><span class="samp">-e</span></samp>&rsquo;,
+<samp><span class="command">grep</span></samp> would attempt to parse 
&lsquo;<samp><span class="samp">--cut here--</span></samp>&rsquo; as a list of
+     <li>Suppose I want to search for a whole word, not a part of a word?
+     <pre class="example">          grep -w 'hello' *
+     <p class="noindent">searches only for instances of &lsquo;<samp><span 
class="samp">hello</span></samp>&rsquo; that are entire words;
+it does not match &lsquo;<samp><span 
+For more control, use &lsquo;<samp><span 
class="samp">\&lt;</span></samp>&rsquo; and
+&lsquo;<samp><span class="samp">\&gt;</span></samp>&rsquo; to match the start 
and end of words. 
+For example:
+     <pre class="example">          grep 'hello\&gt;' *
+     <p class="noindent">searches only for words ending in &lsquo;<samp><span 
class="samp">hello</span></samp>&rsquo;, so it matches the word
+&lsquo;<samp><span class="samp">Othello</span></samp>&rsquo;.
+     <li>How do I output context around the matching lines?
+     <pre class="example">          grep -C 2 'hello' *
+     <p class="noindent">prints two lines of context around each matching line.
+     <li>How do I force <samp><span class="command">grep</span></samp> to 
print the name of the file?
+     <p>Append <samp><span class="file">/dev/null</span></samp>:
+     <pre class="example">          grep 'eli' /etc/passwd /dev/null
+     <p>gets you:
+     <pre class="example">          /etc/passwd:eli:x:2098:1000:Eli 
+     <p>Alternatively, use &lsquo;<samp><span 
class="samp">-H</span></samp>&rsquo;, which is a <span class="sc">gnu</span> 
+     <pre class="example">          grep -H 'eli' /etc/passwd
+     <li>Why do people use strange regular expressions on <samp><span 
class="command">ps</span></samp> output?
+     <pre class="example">          ps -ef | grep '[c]ron'
+     <p>If the pattern had been written without the square brackets, it would
+have matched not only the <samp><span class="command">ps</span></samp> output 
line for <samp><span class="command">cron</span></samp>,
+but also the <samp><span class="command">ps</span></samp> output line for 
<samp><span class="command">grep</span></samp>. 
+Note that on some platforms,
+<samp><span class="command">ps</span></samp> limits the output to the width of 
the screen;
+<samp><span class="command">grep</span></samp> does not have any limit on the 
length of a line
+except the available memory.
+     <li>Why does <samp><span class="command">grep</span></samp> report 
&ldquo;Binary file matches&rdquo;?
+     <p>If <samp><span class="command">grep</span></samp> listed all matching 
&ldquo;lines&rdquo; from a binary file, it
+would probably generate output that is not useful, and it might even
+muck up your display. 
+So <span class="sc">gnu</span> <samp><span class="command">grep</span></samp> 
suppresses output from
+files that appear to be binary files. 
+To force <span class="sc">gnu</span> <samp><span 
+to output lines even from files that appear to be binary, use the
+&lsquo;<samp><span class="samp">-a</span></samp>&rsquo; or &lsquo;<samp><span 
class="samp">--binary-files=text</span></samp>&rsquo; option. 
+To eliminate the
+&ldquo;Binary file matches&rdquo; messages, use the &lsquo;<samp><span 
class="samp">-I</span></samp>&rsquo; or
class="samp">--binary-files=without-match</span></samp>&rsquo; option.
+     <li>Why doesn't &lsquo;<samp><span class="samp">grep 
-lv</span></samp>&rsquo; print non-matching file names?
+     <p>&lsquo;<samp><span class="samp">grep -lv</span></samp>&rsquo; lists 
the names of all files containing one or more
+lines that do not match. 
+To list the names of all files that contain no
+matching lines, use the &lsquo;<samp><span 
class="samp">-L</span></samp>&rsquo; or &lsquo;<samp><span 
+     <li>I can do <span class="sc">or</span> with &lsquo;<samp><span 
class="samp">|</span></samp>&rsquo;, but what about <span class="sc">and</span>?
+     <pre class="example">          grep 'paul' /etc/motd | grep 'franc,ois'
+     <p class="noindent">finds all lines that contain both &lsquo;<samp><span 
class="samp">paul</span></samp>&rsquo; and &lsquo;<samp><span 
+     <li>How can I search in both standard input and in files?
+     <p>Use the special file name &lsquo;<samp><span 
+     <pre class="example">          cat /etc/passwd | grep 'alain' - /etc/motd
+     <li><a name="index-palindromes-244"></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:
+     <pre class="example">          grep -w -e '\(.\)\(.\).\2\1' file
+     <p>It matches the word "radar" or "civic".
+     <p>Guglielmo Bondioni proposed a single RE
+that finds all palindromes up to 19 characters long
+using 9&nbsp;subexpressions<!-- /@w --> and 9&nbsp;back-references<!-- /@w -->:
+     <pre class="smallexample">          grep -E -e 
'^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\9\8\7\6\5\4\3\2\1$' file
+     <p>Note this is done by using <span class="sc">gnu</span> ERE extensions;
+it might not be portable to other implementations of <samp><span 
+     <li>Why is this back-reference failing?
+     <pre class="example">          echo 'ba' | grep -E '(a)\1|b\1'
+     <p>This gives no output, because the first alternate &lsquo;<samp><span 
class="samp">(a)\1</span></samp>&rsquo; does not match,
+as there is no &lsquo;<samp><span class="samp">aa</span></samp>&rsquo; in the 
input, so the &lsquo;<samp><span class="samp">\1</span></samp>&rsquo; in the 
second alternate
+has nothing to refer back to, meaning it will never match anything. 
+(The second alternate in this example can only match
+if the first alternate has matched &ndash; making the second one superfluous.)
+     <li>What do <samp><span class="command">grep</span></samp>, <samp><span 
class="command">fgrep</span></samp>, and <samp><span 
class="command">egrep</span></samp> stand for?
+     <p>The name <samp><span class="command">grep</span></samp> comes from the 
way line editing was done on Unix. 
+For example,
+<samp><span class="command">ed</span></samp> uses the following syntax
+to print a list of matching lines on the screen:
+     <pre class="example">          global/regular expression/print
+          g/re/p
+     <p><samp><span class="command">fgrep</span></samp> stands for Fixed 
<samp><span class="command">grep</span></samp>;
+<samp><span class="command">egrep</span></samp> stands for Extended 
<samp><span class="command">grep</span></samp>.
+        </ol>
+   </body></html>

reply via email to

[Prev in Thread] Current Thread [Next in Thread]