📄 unx06.htm
字号:
<BR></P>
<P>When used as part of an "unanchored" regular expression, that idiomatic regular expression matches the longest string that fits the description, as in the following example:
<BR></P>
<PRE>$ grep 'C.*1' REfile
shell metacharacters we discussed in Chapter 1.</PRE>
<P>The regular expression C.*1 matches the longest string that begins with a C and ends with a 1.
<BR></P>
<P>Another expression, d.*d, matches the longest string that begins and ends with a d. On each line of output in the following example, the matched string is highlighted with italics:
<BR></P>
<PRE>$ grep 'd.*d' REfile
from the set of uppercase an<I>d lowercase letters, d</I>igits,
shell metacharacters we <I>discussed</I> in Chapter 1.
includes only letters. For example, the would match only
words, wherever the regular expression pattern is found
— even if it is surroun<I>ded</I> by other characters — it is</PRE>
<P>You've seen that a regular expression command such as grep finds a match even if the regular expression is surrounded by other characters. For example, the pattern
<BR></P>
<PRE>[Tt]he</PRE>
<P>matches the, The, there, There, other, oTher, and so on (even though the last word is unlikely to be used). Suppose that you're looking for the word The or the and do not want to match other, There, or there. In a few of the commands that use full
regular expressions, you can surround the regular expression with escaped angle brackets (\<___\>). For example, the pattern
<BR></P>
<PRE>\<the\></PRE>
<P>represents the string the, where t follows a character that is not a letter, digit, or underscore, and where e is followed by a character that is not a letter, digit, or underscore. If you need not completely isolate letters, digits, and underscores,
you can use the angle brackets singly. That is, the pattern \<the matches anything that begins with the, and ter\> matches anything that ends with ter.
<BR></P>
<P>You can tell egrep (but not grep) to search for either of two regular expressions as follows:
<BR></P>
<PRE>$ egrep 'regular expression-1 | regular expression-2' filename</PRE>
<H5 ALIGN="CENTER">
<CENTER><A ID="I11" NAME="I11">
<FONT SIZE=3><B>Regular Expression Examples</B>
<BR></FONT></A></CENTER></H5>
<P>When you first look at the list of special characters used with regular expressions, constructing search-and-replacement patterns seems to be a complex process. A few examples and exercises, however, can make the process easier to understand.
<BR></P>
<H6 ALIGN="CENTER">
<CENTER>
<FONT SIZE=3><B>Example 1: Matching Lines That Contain a Date</B>
<BR></FONT></CENTER></H6>
<P>A standard USA date consists of a pattern that includes the capitalized name of a month, a space, a one- or two-digit number representing the day, a comma, a space, and a four-digit number representing the year. For example, Feb 9, 1994 is a standard
USA date. You can write that pattern as a regular expression:
<BR></P>
<PRE>[A-Z][a-z]* [0-9]\{1,2\}, [0-9]\{4\}</PRE>
<P>You can improve this pattern so that it recognizes that May—the month with the shortest name—has three letters, and that September has nine:
<BR></P>
<PRE>[A-Z][a-z]\{3,9\} [0-9]\{1,2\}, [0-9]\{4\}</PRE>
<H6 ALIGN="CENTER">
<CENTER>
<FONT SIZE=3><B>Example 2: Matching Social Security Numbers</B>
<BR></FONT></CENTER></H6>
<P>Social security numbers also are highly structured: three digits, a dash, two digits, a dash, and four digits. Here's how you can write a regular expression for social security numbers:
<BR></P>
<PRE> [0-9]\{3\}-[0-9]\{\2\}-[0-9]\{4\}</PRE>
<H6 ALIGN="CENTER">
<CENTER>
<FONT SIZE=3><B>Example 3: Matching Telephone Numbers</B>
<BR></FONT></CENTER></H6>
<P>Another familiar structured pattern is found in telephone numbers, such as 1-800-555-1212. Here's a regular expression that matches that pattern:
<BR></P>
<PRE>1-[0-9]\{3\}-[0-9]\{3\}-[0-9]\{4\}</PRE>
<P><B>Family</B>
<BR></P>
<P>The grep family consists of three members:
<BR></P>
<TABLE BORDER>
<TR>
<TD>
<P>grep</P>
<TD>
<P>This command uses a limited set of regular expressions. See table.</P>
<TR>
<TD>
<P>egrep</P>
<TD>
<P>Extended grep. This command uses full regular expressions (expressions that have string values and use the full set of alphanumeric and special characters) to match patterns. Full regular expressions include all the limited regular expressions of grep
(except for \( and \)), as well as the following ones (where <I>RE</I> is any regular expression):</P>
<TR>
<TD>
<P>RE+</P>
<TD>
<P>Matches one or more occurrences of RE. (Contrast that with RE*, which matches zero or more occurrences of RE.)</P>
<TR>
<TD>
<P>RE?</P>
<TD>
<P>Matches zero or one occurrence of RE.</P>
<TR>
<TD>
<P><I>RE</I>1 | <I>RE</I>2</P>
<TD>
<P>Matches either <I>RE</I>1 or <I>RE</I>2. The | acts as a logical OR operator.</P>
<TR>
<TD>
<P> (<I>RE</I>)</P>
<TD>
<P>Groups multiple regular expressions.</P>
<TR>
<TD>
<P><BR>]</P>
<TD>
<P>The section "The egrep Command" provides examples of these expressions.</P>
<TR>
<TD>
<P>fgrep</P>
<TD>
<P>Fast grep. This command searches for a string, not a pattern. Because fgrep does not use regular expressions, it interprets $, *, [, ], (, ), and \ as ordinary characters. Modern implementations of grep appear to be just as fast as fgrep, so using fgrep
is becoming obsolete—except when your search involves the previously mentioned characters.</P></TABLE>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> The $, *, [, ], (, ), and \ regular expression metacharacters also have special meaning to the shell, so you must enclose them within single quotation marks to prevent the shell from interpreting
them. (See Chapters 11, 12, and 13.)
<BR></NOTE>
<HR ALIGN=CENTER>
<H5 ALIGN="CENTER">
<CENTER><A ID="I12" NAME="I12">
<FONT SIZE=3><B>The </B><B>grep</B><B> Command</B>
<BR></FONT></A></CENTER></H5>
<P>The most frequently used command in the family is grep. Its complete syntax is
<BR></P>
<PRE>$grep [options] RE [file(s)]</PRE>
<P>where <I>RE</I> is a limited regular expression. Table 6.5 lists the regular expressions that grep recognizes.
<BR></P>
<P>The grep command reads from the specified file on the command line or, if no files are specified, from standard input. Table 6.5 lists the command-line options that grep takes.
<BR></P>
<UL>
<LH><B>Table 6.5. Command-Line Options for </B><B>grep</B>
<BR></LH></UL>
<TABLE BORDER>
<TR>
<TD>
<PRE><I>Option</I>
<BR></PRE>
<TD>
<PRE><I>Result</I>
<BR></PRE>
<TR>
<TD>
<P>-b</P>
<TD>
<P>Display, at the beginning of the output line, the number of the block in which the regular expression was found. This can be helpful in locating block numbers by context. (The first block is block zero.)</P>
<TR>
<TD>
<P>-c</P>
<TD>
<P>Print the number of lines that contain the pattern, that is, the number of matching lines.</P>
<TR>
<TD>
<P>-h</P>
<TD>
<P>Prevent the name of the file that contains the matching line from being displayed at the beginning of that line. NOTE: When searching multiple files, grep normally reports not only the matching line but also the name of the file that contains it.</P>
<TR>
<TD>
<P>-i</P>
<TD>
<P>Ignore distinctions between uppercase and lowercase during comparisons.</P>
<TR>
<TD>
<P>-l</P>
<TD>
<P>Print one time the name of each file that contains lines that match the pattern—regardless of the actual number of matching lines in each file—on separate lines of the screen.</P>
<TR>
<TD>
<P>-n</P>
<TD>
<P>Precede each matching line by its line number in the file.</P>
<TR>
<TD>
<P>-s</P>
<TD>
<P>Suppress error messages about nonexistent or unreadable files.</P>
<TR>
<TD>
<P>-v</P>
<TD>
<P>Print all lines except those that contain the pattern. This reverses the logic of the search.</P></TABLE>
<P>Here are two sample files on which to exercise grep:
<BR></P>
<PRE>$ cat cron
In SCO Xenix 2.3, or SCO UNIX, you can edit a
crontab file to your heart's content, but it will
not be re-read, and your changes will not take
effect, until you come out of multi-user run
level (thus killing cron), and then re-enter
multi-user run level, when a new cron is started;
or until you do a reboot.
The proper way to install a new version of a
crontab (for root, or for any other user) is to
issue the command "crontab new.jobs", or "cat
new.jobs | crontab", or if in 'vi' with a new
version of the commands, "w ! crontab". I find it
easy to type "vi /tmp/tbl", then ":0 r !crontab
-l" to read the existing crontab into the vi
buffer, then edit, then type ":w !crontab", or
"!crontab %" to replace the existing crontab with
what I see on vi's screen.
$ cat pax
This is an announcement for the MS-DOS version of
PAX version 2. See the README file and the man
pages for more information on how to run PAX,
TAR, and CPIO.
For those of you who don't know, pax is a 3 in 1
program that gives the functionality of pax, tar,
and cpio. It supports both the DOS filesystem
and the raw "tape on a disk" system used by most
micro UNIX systems. This will allow for easy
transfer of files to and from UNIX systems. It
also supports multiple volumes. Floppy density
for raw UNIX type read/writes can be specified on
the command line.
The source will eventually be posted to one of
the source groups.
Be sure to use a blocking factor of 20 with
pax-as-tar and B with pax-as-cpio for best
performance.</PRE>
<P>The following examples show how to find a string in a file:
<BR></P>
<PRE>$ grep 'you' pax
For those of you who don't know, pax is a 3 in 1
$ grep 'you' cron
In SCO Xenix 2.3, or SCO UNIX, you can edit a
crontab file to your heart's content, but it will
not be re-read, and your changes will not take
effect, until you come out of multi-user run
or until you do a reboot.</PRE>
<P>Note that you appears in your in the second and third lines.
<BR></P>
<P>You can find the same string in two or more files by using a variety of options. In this first example, case is ignored:
<BR></P>
<PRE>$ grep -i 'you' pax cron
pax:For those of you who don't know, pax is a 3 in 1
cron:In SCO Xenix 2.3, or SCO UNIX, you can edit a
cron:crontab file to your heart's content, but it will
cron:not be re-read, and your changes will not take
cron:effect, until you come out of multi-user run
cron:or until you do a reboot.</PRE>
<P>Notice that each line of output begins with the name of the file that contains a match. In the following example, the output includes the name of the file and the number of the line of that file on which the match is found:
<BR></P>
<PRE>$ grep -n 'you' pax cron
pax:6:For those of you who don't know, pax is a 3 in 1
cron:1:In SCO Xenix 2.3, or SCO UNIX, you can edit a
cron:2:crontab file to your heart's content, but it will
cron:3:not be re-read, and your changes will not take
cron:4:effect, until you come out of multi-user run
cron:7:or until you do a reboot.</PRE>
<P>The following example shows how to inhibit printing the lines themselves:
<BR></P>
<PRE>$ grep -c 'you' pax cron
pax:1
cron:5</PRE>
<P>The following output shows the matching lines without specifying the files from which they came:
<BR></P>
<PRE>$ grep -h 'you' pax cron
For those of you who don't know, pax is a 3 in 1
In SCO Xenix 2.3, or SCO UNIX, you can edit a
crontab file to your heart's content, but it will
not be re-read, and your changes will not take
effect, until you come out of multi-user run
or until you do a reboot.</PRE>
<P>The following specifies output of "every line in pax and cron that does not have [Yy][Oo][Uu] in it":
<BR></P>
<PRE>$ grep -iv 'you' pax cron
pax:This is an announcement for the MS-DOS version of
pax:PAX version 2. See the README file and the man
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -