[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">
+<head>
+<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
Bugs">
+<link href="http://www.gnu.org/software/texinfo/" 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 }
+ span.sc { font-variant:small-caps }
+ span.roman { font-family:serif; font-weight:normal; }
+ span.sansserif { font-family:sans-serif; font-weight:normal; }
+--></style>
+</head>
+<body>
+<div class="node">
+<a name="Usage"></a>
+<p>
+Next: <a rel="next" accesskey="n"
href="Reporting-Bugs.html#Reporting-Bugs">Reporting Bugs</a>,
+Previous: <a rel="previous" accesskey="p"
href="Regular-Expressions.html#Regular-Expressions">Regular Expressions</a>,
+Up: <a rel="up" accesskey="u" href="index.html#Top">Top</a>
+<hr>
+</div>
+
+<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
class="command">grep</span></samp>:
+
+<pre class="example"> grep -i 'hello.*world' menu.h main.c
+</pre>
+ <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 ‘<samp><span class="samp">hello</span></samp>’
followed by the string ‘<samp><span
class="samp">world</span></samp>’;
+this is because ‘<samp><span class="samp">.*</span></samp>’
matches zero or more characters within a line.
+See <a href="Regular-Expressions.html#Regular-Expressions">Regular
Expressions</a>.
+The ‘<samp><span class="samp">-i</span></samp>’ option causes
<samp><span class="command">grep</span></samp>
+to ignore case, causing it to match the line ‘<samp><span
class="samp">Hello, world!</span></samp>’, 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
+</pre>
+ <p class="noindent">lists the names of all C files in the current
directory whose contents
+mention ‘<samp><span class="samp">main</span></samp>’.
+
+ <li>How do I search directories recursively?
+
+ <pre class="example"> grep -r 'hello' /home/gigi
+</pre>
+ <p class="noindent">searches for ‘<samp><span
class="samp">hello</span></samp>’ 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
class="command">xargs</span></samp>.
+For example, the following command searches only C files:
+
+ <pre class="example"> find /home/gigi -name '*.c' -print0 |
xargs -0r grep -H 'hello'
+</pre>
+ <p>This differs from the command:
+
+ <pre class="example"> grep -rH 'hello' *.c
+</pre>
+ <p>which merely looks for ‘<samp><span
class="samp">hello</span></samp>’ in all files in the current
+directory whose names end in ‘<samp><span
class="samp">.c</span></samp>’.
+Here the <samp><span class="option">-r</span></samp> is
+probably unnecessary, as recursion occurs only in the unlikely event
+that one of ‘<samp><span class="samp">.c</span></samp>’ files is a
directory.
+The ‘<samp><span class="samp">find ...</span></samp>’ command line
above is more similar to the command:
+
+ <pre class="example"> grep -rH --include='*.c' 'hello' /home/gigi
+</pre>
+ <li>What if a pattern has a leading ‘<samp><span
class="samp">-</span></samp>’?
+
+ <pre class="example"> grep -e '--cut here--' *
+</pre>
+ <p class="noindent">searches for all lines matching ‘<samp><span
class="samp">--cut here--</span></samp>’.
+Without ‘<samp><span class="samp">-e</span></samp>’,
+<samp><span class="command">grep</span></samp> would attempt to parse
‘<samp><span class="samp">--cut here--</span></samp>’ as a list of
+options.
+
+ <li>Suppose I want to search for a whole word, not a part of a word?
+
+ <pre class="example"> grep -w 'hello' *
+</pre>
+ <p class="noindent">searches only for instances of ‘<samp><span
class="samp">hello</span></samp>’ that are entire words;
+it does not match ‘<samp><span
class="samp">Othello</span></samp>’.
+For more control, use ‘<samp><span
class="samp">\<</span></samp>’ and
+‘<samp><span class="samp">\></span></samp>’ to match the start
and end of words.
+For example:
+
+ <pre class="example"> grep 'hello\>' *
+</pre>
+ <p class="noindent">searches only for words ending in ‘<samp><span
class="samp">hello</span></samp>’, so it matches the word
+‘<samp><span class="samp">Othello</span></samp>’.
+
+ <li>How do I output context around the matching lines?
+
+ <pre class="example"> grep -C 2 'hello' *
+</pre>
+ <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
+</pre>
+ <p>gets you:
+
+ <pre class="example"> /etc/passwd:eli:x:2098:1000:Eli
Smith:/home/eli:/bin/bash
+</pre>
+ <p>Alternatively, use ‘<samp><span
class="samp">-H</span></samp>’, which is a <span class="sc">gnu</span>
extension:
+
+ <pre class="example"> grep -H 'eli' /etc/passwd
+</pre>
+ <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'
+</pre>
+ <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
“Binary file matches”?
+
+ <p>If <samp><span class="command">grep</span></samp> listed all matching
“lines” 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
class="command">grep</span></samp>
+to output lines even from files that appear to be binary, use the
+‘<samp><span class="samp">-a</span></samp>’ or ‘<samp><span
class="samp">--binary-files=text</span></samp>’ option.
+To eliminate the
+“Binary file matches” messages, use the ‘<samp><span
class="samp">-I</span></samp>’ or
+‘<samp><span
class="samp">--binary-files=without-match</span></samp>’ option.
+
+ <li>Why doesn't ‘<samp><span class="samp">grep
-lv</span></samp>’ print non-matching file names?
+
+ <p>‘<samp><span class="samp">grep -lv</span></samp>’ 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 ‘<samp><span
class="samp">-L</span></samp>’ or ‘<samp><span
class="samp">--files-without-match</span></samp>’
+option.
+
+ <li>I can do <span class="sc">or</span> with ‘<samp><span
class="samp">|</span></samp>’, but what about <span class="sc">and</span>?
+
+ <pre class="example"> grep 'paul' /etc/motd | grep 'franc,ois'
+</pre>
+ <p class="noindent">finds all lines that contain both ‘<samp><span
class="samp">paul</span></samp>’ and ‘<samp><span
class="samp">franc,ois</span></samp>’.
+
+ <li>How can I search in both standard input and in files?
+
+ <p>Use the special file name ‘<samp><span
class="samp">-</span></samp>’:
+
+ <pre class="example"> cat /etc/passwd | grep 'alain' - /etc/motd
+</pre>
+ <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
+</pre>
+ <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 subexpressions<!-- /@w --> and 9 back-references<!-- /@w -->:
+
+ <pre class="smallexample"> grep -E -e
'^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\9\8\7\6\5\4\3\2\1$' file
+</pre>
+ <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
class="command">grep</span></samp>.
+
+ <li>Why is this back-reference failing?
+
+ <pre class="example"> echo 'ba' | grep -E '(a)\1|b\1'
+</pre>
+ <p>This gives no output, because the first alternate ‘<samp><span
class="samp">(a)\1</span></samp>’ does not match,
+as there is no ‘<samp><span class="samp">aa</span></samp>’ in the
input, so the ‘<samp><span class="samp">\1</span></samp>’ 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 – 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
+</pre>
+ <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>
+
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Changes to manual/html_node/Usage.html,
Karl Berry <=