CVSROOT: /web/grep Module name: grep Changes by: Karl Berry <karl> 09/05/24 18:43:01
Index: doc/grep_13.html =================================================================== RCS file: doc/grep_13.html diff -N doc/grep_13.html --- doc/grep_13.html 23 Jan 2002 10:33:52 -0000 1.3 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,300 +0,0 @@ -<HTML> -<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> -<!-- Created on January, 23 2002 by texi2html 1.64 --> -<!-- -Written by: Lionel Cons <[email protected]> (original author) - Karl Berry <[email protected]> - Olaf Bachmann <[email protected]> - and many others. -Maintained by: Olaf Bachmann <[email protected]> -Send bugs and suggestions to <[email protected]> - ---> -<HEAD> -<TITLE>grep, print lines matching a pattern: Usage</TITLE> - -<META NAME="description" CONTENT="grep, print lines matching a pattern: Usage"> -<META NAME="keywords" CONTENT="grep, print lines matching a pattern: Usage"> -<META NAME="resource-type" CONTENT="document"> -<META NAME="distribution" CONTENT="global"> -<META NAME="Generator" CONTENT="texi2html 1.64"> - -</HEAD> - -<BODY LANG="" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000"> - -<A NAME="SEC13"></A> -<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0> -<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep_12.html#SEC12"> < </A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep_14.html#SEC14"> > </A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep_14.html#SEC14"> << </A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep.html#SEC_Top"> Up </A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep_14.html#SEC14"> >> </A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep.html#SEC_Top">Top</A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep_toc.html#SEC_Contents">Contents</A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep_21.html#SEC21">Index</A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep_abt.html#SEC_About"> ? </A>]</TD> -</TR></TABLE> -<HR SIZE=1> -<H1> 6. Usage </H1> -<!--docid::SEC13::--> -<P> - -<A NAME="IDX209"></A> -Here is an example shell command that invokes GNU <CODE>grep</CODE>: -</P><P> - -<TABLE><tr><td> </td><td class=example><pre>grep -i 'hello.*world' menu.h main.c -</pre></td></tr></table></P><P> - -This lists all lines in the files <TT>`menu.h'</TT> and <TT>`main.c'</TT> that -contain the string <SAMP>`hello'</SAMP> followed by the string <SAMP>`world'</SAMP>; -this is because <SAMP>`.*'</SAMP> matches zero or more characters within a line. -See section <A HREF="grep_7.html#SEC7">5. Regular Expressions</A>. 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 section <A HREF="grep_2.html#SEC2">2. Invoking <CODE>grep</CODE></A>, for more details about -how to invoke <CODE>grep</CODE>. -</P><P> - -<A NAME="IDX210"></A> -<A NAME="IDX211"></A> -Here are some common questions and answers about <CODE>grep</CODE> usage. -</P><P> - -<OL> - -<LI> -How can I list just the names of matching files? -<P> - -<TABLE><tr><td> </td><td class=example><pre>grep -l 'main' *.c -</pre></td></tr></table></P><P> - -lists the names of all C files in the current directory whose contents -mention <SAMP>`main'</SAMP>. -</P><P> - -<LI> -How do I search directories recursively? -<P> - -<TABLE><tr><td> </td><td class=example><pre>grep -r 'hello' /home/gigi -</pre></td></tr></table></P><P> - -searches for <SAMP>`hello'</SAMP> in all files under the directory -<TT>`/home/gigi'</TT>. For more control of which files are searched, use -<CODE>find</CODE>, <CODE>grep</CODE> and <CODE>xargs</CODE>. For example, -the following command searches only C files: -</P><P> - -<TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>find /home/gigi -name '*.c' -print | xargs grep 'hello' /dev/null -</FONT></pre></td></tr></table></P><P> - -This differs from the command: -</P><P> - -<TABLE><tr><td> </td><td class=example><pre>grep -r 'hello' *.c -</pre></td></tr></table></P><P> - -which merely looks for <SAMP>`hello'</SAMP> in all files in the current -directory whose names end in <SAMP>`.c'</SAMP>. Here the <SAMP>`-r'</SAMP> is -probably unnecessary, as recursion occurs only in the unlikely event -that one of <SAMP>`.c'</SAMP> files is a directory. -</P><P> - -<LI> -What if a pattern has a leading <SAMP>`-'</SAMP>? -<P> - -<TABLE><tr><td> </td><td class=example><pre>grep -e '--cut here--' * -</pre></td></tr></table></P><P> - -searches for all lines matching <SAMP>`--cut here--'</SAMP>. Without <SAMP>`-e'</SAMP>, -<CODE>grep</CODE> would attempt to parse <SAMP>`--cut here--'</SAMP> as a list of -options. -</P><P> - -<LI> -Suppose I want to search for a whole word, not a part of a word? -<P> - -<TABLE><tr><td> </td><td class=example><pre>grep -w 'hello' * -</pre></td></tr></table></P><P> - -searches only for instances of <SAMP>`hello'</SAMP> that are entire words; it -does not match <SAMP>`Othello'</SAMP>. For more control, use <SAMP>`\<'</SAMP> and -<SAMP>`\>'</SAMP> to match the start and end of words. For example: -</P><P> - -<TABLE><tr><td> </td><td class=example><pre>grep 'hello\>' * -</pre></td></tr></table></P><P> - -searches only for words ending in <SAMP>`hello'</SAMP>, so it matches the word -<SAMP>`Othello'</SAMP>. -</P><P> - -<LI> -How do I output context around the matching lines? -<P> - -<TABLE><tr><td> </td><td class=example><pre>grep -C 2 'hello' * -</pre></td></tr></table></P><P> - -prints two lines of context around each matching line. -</P><P> - -<LI> -How do I force grep to print the name of the file? -<P> - -Append <TT>`/dev/null'</TT>: -</P><P> - -<TABLE><tr><td> </td><td class=example><pre>grep 'eli' /etc/passwd /dev/null -</pre></td></tr></table></P><P> - -gets you: -</P><P> - -<TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>/etc/passwd:eli:DNGUTF58.IMe.:98:11:Eli Smith:/home/do/eli:/bin/bash -</FONT></pre></td></tr></table></P><P> - -<LI> -Why do people use strange regular expressions on <CODE>ps</CODE> output? -<P> - -<TABLE><tr><td> </td><td class=example><pre>ps -ef | grep '[c]ron' -</pre></td></tr></table></P><P> - -If the pattern had been written without the square brackets, it would -have matched not only the <CODE>ps</CODE> output line for <CODE>cron</CODE>, -but also the <CODE>ps</CODE> output line for <CODE>grep</CODE>. -Note that some platforms <CODE>ps</CODE> limit the ouput to the width -of the screen, grep does not have any limit on the length of a line -except the available memory. -</P><P> - -<LI> -Why does <CODE>grep</CODE> report "Binary file matches"? -<P> - -If <CODE>grep</CODE> 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 GNU <CODE>grep</CODE> suppresses output from -files that appear to be binary files. To force GNU <CODE>grep</CODE> -to output lines even from files that appear to be binary, use the -<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. -</P><P> - -<LI> -Why doesn't <SAMP>`grep -lv'</SAMP> print nonmatching file names? -<P> - -<SAMP>`grep -lv'</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>`-L'</SAMP> or <SAMP>`--files-without-match'</SAMP> -option. -</P><P> - -<LI> -I can do OR with <SAMP>`|'</SAMP>, but what about AND? -<P> - -<TABLE><tr><td> </td><td class=example><pre>grep 'paul' /etc/motd | grep 'franc,ois' -</pre></td></tr></table></P><P> - -finds all lines that contain both <SAMP>`paul'</SAMP> and <SAMP>`franc,ois'</SAMP>. -</P><P> - -<LI> -How can I search in both standard input and in files? -<P> - -Use the special file name <SAMP>`-'</SAMP>: -</P><P> - -<TABLE><tr><td> </td><td class=example><pre>cat /etc/passwd | grep 'alain' - /etc/motd -</pre></td></tr></table></P><P> - -<LI> -<A NAME="IDX212"></A> -How to express palindromes in a regular expression? -<P> - -It can be done by using the back referecences, for example a palindrome -of 4 chararcters can be written in BRE. -</P><P> - -<TABLE><tr><td> </td><td class=example><pre>grep -w -e '\(.\)\(.\).\2\1' file -</pre></td></tr></table></P><P> - -It matches the word "radar" or "civic". -</P><P> - -Guglielmo Bondioni proposed a single RE that finds all the palindromes up to 19 -characters long. -</P><P> - -<TABLE><tr><td> </td><td class=example><pre>egrep -e '^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\9\8\7\6\5\4\3\2\1$' file -</pre></td></tr></table></P><P> - -Note this is done by using GNU ERE extensions, it might not be portable on -other greps. -</P><P> - -<LI> -Why are my expressions whith the vertical bar fail? -<P> - -<TABLE><tr><td> </td><td class=example><pre>/bin/echo "ba" | egrep '(a)\1|(b)\1' -</pre></td></tr></table></P><P> - -The first alternate branch fails then the first group was not in the match -this will make the second alternate branch fails. For example, "aaba" will -match, the first group participate in the match and can be reuse in the -second branch. -</P><P> - -<LI> -What do <CODE>grep, fgrep, egrep</CODE> stand for ? -<P> - -grep comes from the way line editing was done on Unix. For example, -<CODE>ed</CODE> uses this syntax to print a list of matching lines on the screen. -</P><P> - -<TABLE><tr><td> </td><td class=example><pre>global/regular expression/print -g/re/p -</pre></td></tr></table></P><P> - -<CODE>fgrep</CODE> stands for Fixed <CODE>grep</CODE>, <CODE>egrep</CODE> Extended -<CODE>grep</CODE>. -</P><P> - -</OL> -<P> - -<A NAME="Reporting Bugs"></A> -<HR SIZE=1> -<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0> -<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep_12.html#SEC12"> < </A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep_14.html#SEC14"> > </A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep_14.html#SEC14"> << </A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep.html#SEC_Top"> Up </A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep_14.html#SEC14"> >> </A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep.html#SEC_Top">Top</A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep_toc.html#SEC_Contents">Contents</A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep_21.html#SEC21">Index</A>]</TD> -<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="grep_abt.html#SEC_About"> ? </A>]</TD> -</TR></TABLE> -<BR> -<FONT SIZE="-1"> -This document was generated -by <I>root</I> on <I>January, 23 2002</I> -using <A HREF="http://www.mathematik.uni-kl.de/~obachman/Texi2html -"><I>texi2html</I></A> - -</BODY> -</HTML>
