📄 0075-0077.html
字号:
<HTML>
<HEAD>
<TITLE>Sams Teach Yourself Linux in 24 Hours:Manipulation and Searching Commands:EarthWeb Inc.-</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311623 //-->
<!-- TITLE=Sams Teach Yourself Linux in 24 Hours//-->
<!-- AUTHOR=Bill Ball//-->
<!-- PUBLISHER=Macmillan Computer Publishing//-->
<!-- IMPRINT=Sams//-->
<!-- CHAPTER=05 //-->
<!-- PAGES=0063-0082 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0072-0074.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0078-0081.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-75"><P>Page 75</P></A>
<P>Obviously, this is not the result you want, because the multiple searches have
printed duplicate filenames. To find exactly what you want, use a regular expression that tells
ls to list any file with a number appearing in the filename, for example:
</P>
<!-- CODE //-->
<PRE>
# ls *[0123456789]*
0001file.0009 32days.msg day67.txt
08100097.db 96hours.txt message.76
14days.txt backup001.file phonelog.111597
</PRE>
<!-- END CODE //-->
<P>This shows all files containing numbers in the filename, because you've specified a
range of characters, or in this case, numbers in your search pattern. You can also use a
regular expression shorthand to build a shorter expression to do the same thing, for example:
</P>
<!-- CODE //-->
<PRE>
# ls *[0-9]*
0001file.0009 32days.msg day67.txt
08100097.db 96hours.txt message.76
14days.txt backup001.file phonelog.111597
</PRE>
<!-- END CODE //-->
<P>How you specify your pattern characters in your expression is important. If you only
want a list of files ending in a number, you can use
</P>
<!-- CODE SNIP //-->
<PRE>
# ls *[0-9]
0001file.0009 message.76 phonelog.111597
</PRE>
<!-- END CODE SNIP //-->
<P>If you only want a list of files beginning with a number, you can use
</P>
<!-- CODE SNIP //-->
<PRE>
# ls [0-9]*
0001file.0009 08100097.db 14days.txt 32days.msg 96hours.txt
</PRE>
<!-- END CODE SNIP //-->
<P>Here's a fun exercise: What if you want to list only those files with numbers inside or
on both ends of a filename? Try these:
</P>
<!-- CODE SNIP //-->
<PRE>
# ls *[-a-z][0-9]*
backup001.file day67.txt
# ls [0-9]*[a-z]*[0-9]
0001file.0009
</PRE>
<!-- END CODE SNIP //-->
<P>Finally, how do you match patterns when the pattern you're looking for contains a
pattern-matching character? Easy! Use the backlash to "escape" the character, for example:
</P>
<!-- CODE SNIP //-->
<PRE>
# ls *\?*
cathy?.message
</PRE>
<!-- END CODE SNIP //-->
<P>As you can see, using regular expressions can take some practice, but your efforts will
be well rewarded. Try experimenting with different expressions to
see if you can come
</P>
<P>up with results similar to the examples shown in this section.
</P>
<H4><A NAME="ch05_ 15">
Searching Inside Files with the grep Commands
</A></H4>
<P>This section introduces you to the family of
grep commands. You'll learn about grep, egrep, and
fgrep. In order to use these commands, you should know how to use some of the
pattern-
</P>
<A NAME="PAGENUM-76"><P>Page 76</P></A>
<P>matching techniques already discussed. You'll use these commands to search through
files and extract text. Each of these programs works by searching each line in a file. You
can search single files or a range of files.
</P>
<P>Each of the grep commands is basically the same and has nearly 20 different
command-line options. The only real difference is that
egrep uses a slightly different syntax for its
pattern matching, whereas the frep command uses fixed strings. You'll see examples of
each program, using some of the common options. For specific details about the
pattern-matching capabilities of these programs, see the
grep manual page.
</P>
<P>To show you the difference in each program's search pattern syntax, I'll look for
different patterns in Matt Welsh's Linux Basic Installation
Guide (available at <a href="http://sunsite.unc.edu/ldp).">
http://sunsite.unc.edu/LDP).</A> For example, if you want to find all lines in the guide that begin with a number, use
the following syntax:
</P>
<!-- CODE //-->
<PRE>
# grep ^[0-9] guide.txt
1 Introduction to Linux 1
2 Obtaining and Installing Linux 40
3 Linux Tutorial 85
4 System Administration 137
...
# egrep ^[0-9] guide.txt
1 Introduction to Linux 1
2 Obtaining and Installing Linux 40
3 Linux Tutorial 85
4 System Administration 137
...
# fgrep ^[0-9] guide.txt
</PRE>
<!-- END CODE //-->
<P>You can see that grep and egrep returned a search (I've deleted all the output except the
first four lines). Notice, however, that fgrep
cannot handle regular expressions. You must use fixed patterns, or strings with the
fgrep command, for example:
</P>
<!-- CODE //-->
<PRE>
# fgrep friend guide.txt
large extent by the window manager. This friendly program is in
copy Linux from a friend who may already have the software, or share
(Unfortunately, the system was being unfriendly.)
</PRE>
<!-- END CODE //-->
<P>Now use egrep to try searching for the pattern of the letter b in parentheses in the file:
</P>
<!-- CODE //-->
<PRE>
# egrep "\([b]\)" guide.txt
(see Section 1.8 for a list of compatible boards), or (b) there is an
connect to the network, or (b) you have a ``dynamic'' IP address,
</PRE>
<!-- END CODE //-->
<P>You see that there are exactly two lines in the file that match (b). See what happens
when you search with grep:
</P>
<!-- CODE //-->
<PRE>
# grep "\([b]\)" guide.txt
This is version 2.2.2 of the book, "Linux Installation and Getting
to PostScript printers. This document was generated by a set of tools
from LaTeX source, so there may be a number of formatting problems.
This is not the "official" version of the book! Please see
...
</PRE>
<!-- END CODE //-->
<A NAME="PAGENUM-77"><P>Page 77</P></A>
<P>Whoa! Not exactly what you wanted, is it? As you can see,
grep does not use the same syntax as the egrep command. But you can use a simpler approach:
</P>
<!-- CODE SNIP //-->
<PRE>
# grep "(b)" guide.txt
(see Section 1.8 for a list of compatible boards), or (b) there is an
connect to the network, or (b) you have a ``dynamic'' IP address,
</PRE>
<!-- END CODE SNIP //-->
<P>This pattern works with grep and fgrep. If you try this pattern with
egrep, you'll get the same results as if you tried extended regular expressions with
grep (each line with a b).
</P>
<P>Each grep program accepts nearly the same set of command-line options. One
popular option is -n, or line numbering. This is handy because you'll see which lines in the
file contain matches. This example works for each
grep program:
</P>
<!-- CODE //-->
<PRE>
# egrep -n "friend" guide.txt
1242:large extent by the window manager. This friendly program is in
1942:copy Linux from a friend who may already have the software, or share
5161:(Unfortunately, the system was being unfriendly.)
</PRE>
<!-- END CODE //-->
<P>You can see that matches were made on lines
1242, 1942, and 5161. Another feature of these programs is that you don't have to retype your patterns each time you want to search.
As a simple example, if you need to repeatedly search files for different words, you can
put these into a file for grep to use. First, create a text file, then use the
-f option to specify the file:
</P>
<!-- CODE //-->
<PRE>
# cat > mywords
wonderful
Typewriter
War
# grep -nf mywords guide.txt
574:Typewriter Used to represent screen interaction, as in
617:software since the original Space War, or, more recently, Emacs. It
1998:Now you must be convinced of how wonderful Linux is, and all of the
2549:inanimate object is a wonderful way to relieve the occasional stress
3790: Warning: Linux cannot currently use 33090 sectors of this
7780:to wear the magic hat when it is not needed, despite the wonderful
10091:wonderful programs and configurations are available with a bit of work
</PRE>
<!-- END CODE //-->
<P>Because you also used the line-numbering option, you should note that it had to
come before the -f, or file option, or grep would report an error, complain it couldn't find file
n, and quit.
</P>
<P>You can make grep act like fgrep with the -F option, or like
egrep with the -E option. You'll also find a unique version of
grep on your system, called zgrep, which you can use to
search compressed files, the topic of the next section.
</P>
<H3><A NAME="ch05_ 16">
Compressing and Uncompressing Files
</A></H3>
<P><CENTER>
<a href="0072-0074.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0078-0081.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -