464 lines
6.1 KiB
HTML
464 lines
6.1 KiB
HTML
<HTML
|
|
><HEAD
|
|
><TITLE
|
|
>Finding Text Within Files</TITLE
|
|
><META
|
|
NAME="GENERATOR"
|
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
|
|
REL="HOME"
|
|
TITLE="GNU/Linux Command-Line Tools Summary"
|
|
HREF="book1.htm"><LINK
|
|
REL="UP"
|
|
TITLE="Text Related Tools"
|
|
HREF="c6435.htm"><LINK
|
|
REL="PREVIOUS"
|
|
TITLE="Text Conversion/Filter Tools"
|
|
HREF="x7619.htm"><LINK
|
|
REL="NEXT"
|
|
TITLE="Mathematical tools"
|
|
HREF="c8113.htm"></HEAD
|
|
><BODY
|
|
CLASS="SECT1"
|
|
BGCOLOR="#FFFFFF"
|
|
TEXT="#000000"
|
|
LINK="#0000FF"
|
|
VLINK="#840084"
|
|
ALINK="#0000FF"
|
|
><DIV
|
|
CLASS="NAVHEADER"
|
|
><TABLE
|
|
SUMMARY="Header navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TH
|
|
COLSPAN="3"
|
|
ALIGN="center"
|
|
>GNU/Linux Command-Line Tools Summary</TH
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="left"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="x7619.htm"
|
|
ACCESSKEY="P"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="80%"
|
|
ALIGN="center"
|
|
VALIGN="bottom"
|
|
>Chapter 11. Text Related Tools</TD
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="right"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="c8113.htm"
|
|
ACCESSKEY="N"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"></DIV
|
|
><DIV
|
|
CLASS="SECT1"
|
|
><H1
|
|
CLASS="SECT1"
|
|
><A
|
|
NAME="FINDING-TEXT-WITHIN-FILES"
|
|
></A
|
|
>Finding Text Within Files</H1
|
|
><P
|
|
></P
|
|
><DIV
|
|
CLASS="VARIABLELIST"
|
|
><DL
|
|
><DT
|
|
>grep</DT
|
|
><DD
|
|
><P
|
|
><A
|
|
NAME="AEN7976"
|
|
></A
|
|
>Looks for text within files. For example:</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
><SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>grep this_word this_file.txt</I
|
|
></SPAN
|
|
> </PRE
|
|
><P
|
|
>Example options:</P
|
|
><P
|
|
></P
|
|
><UL
|
|
><LI
|
|
><P
|
|
><SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>-v<A
|
|
NAME="AEN7985"
|
|
></A
|
|
></I
|
|
></SPAN
|
|
> --- this option is used to display lines <A
|
|
NAME="AEN7987"
|
|
></A
|
|
>which do not contain the string.</P
|
|
></LI
|
|
><LI
|
|
><P
|
|
><SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>-n<A
|
|
NAME="AEN7992"
|
|
></A
|
|
></I
|
|
></SPAN
|
|
> --- this option displays the line <A
|
|
NAME="AEN7994"
|
|
></A
|
|
>numbers</P
|
|
></LI
|
|
><LI
|
|
><P
|
|
><SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>-w<A
|
|
NAME="AEN7999"
|
|
></A
|
|
></I
|
|
></SPAN
|
|
> --- this option makes grep <A
|
|
NAME="AEN8001"
|
|
></A
|
|
>match the whole word </P
|
|
></LI
|
|
><LI
|
|
><P
|
|
><SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>-A x </I
|
|
></SPAN
|
|
>or<SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
> -B </I
|
|
></SPAN
|
|
>x<A
|
|
NAME="AEN8007"
|
|
></A
|
|
><A
|
|
NAME="AEN8009"
|
|
></A
|
|
> (where x is a number) --- display <A
|
|
NAME="AEN8011"
|
|
></A
|
|
>“x” lines After or Before the section <A
|
|
NAME="AEN8013"
|
|
></A
|
|
>where the particular <A
|
|
NAME="AEN8015"
|
|
></A
|
|
>word is found.<A
|
|
NAME="AEN8017"
|
|
></A
|
|
> </P
|
|
></LI
|
|
><LI
|
|
><P
|
|
><SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>-r<A
|
|
NAME="AEN8022"
|
|
></A
|
|
></I
|
|
></SPAN
|
|
> or <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>rgrep</I
|
|
></SPAN
|
|
> --- search for text <A
|
|
NAME="AEN8025"
|
|
></A
|
|
>within files recursively. </P
|
|
></LI
|
|
></UL
|
|
><P
|
|
>This command uses regular expressions<A
|
|
NAME="AEN8028"
|
|
></A
|
|
>, for more information <A
|
|
NAME="AEN8030"
|
|
></A
|
|
>please see, <A
|
|
HREF="x11655.htm#REGULAR-EXPRESSIONS"
|
|
>the Section called <I
|
|
>Regular Expressions</I
|
|
> in Chapter 20</A
|
|
>.</P
|
|
><P
|
|
>For example, this command would look <A
|
|
NAME="AEN8034"
|
|
></A
|
|
>in the file “rpmlist.txt” for anything starting <A
|
|
NAME="AEN8036"
|
|
></A
|
|
>with “rpm”:</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>grep rpm rpmlist.txt</PRE
|
|
><P
|
|
>Or you could use it like this, to search through the output <A
|
|
NAME="AEN8040"
|
|
></A
|
|
>of another file:</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>rpm -qa | grep ogg<A
|
|
NAME="AEN8043"
|
|
></A
|
|
><A
|
|
NAME="AEN8045"
|
|
></A
|
|
></PRE
|
|
><P
|
|
>The first command lists <A
|
|
NAME="AEN8048"
|
|
></A
|
|
>all RPM's installed <A
|
|
NAME="AEN8050"
|
|
></A
|
|
>on your system, the second finds<A
|
|
NAME="AEN8052"
|
|
></A
|
|
> any containing the string “ogg” and outputs <A
|
|
NAME="AEN8054"
|
|
></A
|
|
>them.</P
|
|
></DD
|
|
><DT
|
|
>rgrep</DT
|
|
><DD
|
|
><P
|
|
><A
|
|
NAME="AEN8060"
|
|
></A
|
|
>A "recursive"<A
|
|
NAME="AEN8062"
|
|
></A
|
|
> version <A
|
|
NAME="AEN8064"
|
|
></A
|
|
>of <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>grep</I
|
|
></SPAN
|
|
> (this is a different <A
|
|
NAME="AEN8067"
|
|
></A
|
|
>program to <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>grep</I
|
|
></SPAN
|
|
>). This will search all the files in the current directory <A
|
|
NAME="AEN8070"
|
|
></A
|
|
>and all it's subdirectories and print <A
|
|
NAME="AEN8072"
|
|
></A
|
|
>the names of the files and the matching <A
|
|
NAME="AEN8074"
|
|
></A
|
|
>line. Follows <A
|
|
NAME="AEN8076"
|
|
></A
|
|
>similar syntax <A
|
|
NAME="AEN8078"
|
|
></A
|
|
>to <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>grep</I
|
|
></SPAN
|
|
> (see above). You could also use <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>grep</I
|
|
></SPAN
|
|
> with the <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>-r</I
|
|
></SPAN
|
|
> option <A
|
|
NAME="AEN8083"
|
|
></A
|
|
>to achieve the same affect.<A
|
|
NAME="AEN8085"
|
|
></A
|
|
></P
|
|
></DD
|
|
><DT
|
|
>fgrep</DT
|
|
><DD
|
|
><P
|
|
><A
|
|
NAME="AEN8091"
|
|
></A
|
|
>This version <A
|
|
NAME="AEN8093"
|
|
></A
|
|
>of <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>grep</I
|
|
></SPAN
|
|
> calls <A
|
|
NAME="AEN8096"
|
|
></A
|
|
><SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>grep</I
|
|
></SPAN
|
|
> with the <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>-F</I
|
|
></SPAN
|
|
> option. This will look for literal <A
|
|
NAME="AEN8100"
|
|
></A
|
|
>strings only, it won't use or expand <A
|
|
NAME="AEN8102"
|
|
></A
|
|
>any kind of regular <A
|
|
NAME="AEN8104"
|
|
></A
|
|
>expression.</P
|
|
><P
|
|
>For example you could type:</P
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>fgrep 'a$*b?' file.txt</PRE
|
|
><P
|
|
>And <SPAN
|
|
CLASS="emphasis"
|
|
><I
|
|
CLASS="EMPHASIS"
|
|
>fgrep</I
|
|
></SPAN
|
|
> would look for the string “a$*b?” in the file “file.txt”.</P
|
|
></DD
|
|
></DL
|
|
></DIV
|
|
><DIV
|
|
CLASS="TIP"
|
|
><BLOCKQUOTE
|
|
CLASS="TIP"
|
|
><P
|
|
><B
|
|
>Other Versions: </B
|
|
>There are various versions of grep which are designed to do different things try searching for them on the internet or within your distribution.</P
|
|
></BLOCKQUOTE
|
|
></DIV
|
|
></DIV
|
|
><DIV
|
|
CLASS="NAVFOOTER"
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"><TABLE
|
|
SUMMARY="Footer navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="x7619.htm"
|
|
ACCESSKEY="P"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="book1.htm"
|
|
ACCESSKEY="H"
|
|
>Home</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="c8113.htm"
|
|
ACCESSKEY="N"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
>Text Conversion/Filter Tools</TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="c6435.htm"
|
|
ACCESSKEY="U"
|
|
>Up</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
>Mathematical tools</TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
></BODY
|
|
></HTML
|
|
> |