⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unx06.htm

📁 Linux Unix揭密.高质量电子书籍.对学习Linux有大帮助,欢迎下载学习.
💻 HTM
📖 第 1 页 / 共 5 页
字号:

<P>Display the number of the current line.</P>

<TR>

<TD>

<P>v</P>

<TD>

<P>Drop into the editor (see Chapter 7) indicated by the EDITOR environment variable (see Chapters 11, 12, 13), at the current line of the current file.</P>

<TR>

<TD>

<P>h</P>

<TD>

<P>Display a Help screen that describes all the more or page commands.</P>

<TR>

<TD>

<P>:f </P>

<TD>

<P>Display the name and current line number of the file that you are viewing.</P>

<TR>

<TD>

<P>:q, :Q</P>

<TD>

<P>Exit from more or page (same as q or Q).</P>

<TR>

<TD>

<P>_  (dot) </P>

<TD>

<P>Repeat the previous command.</P></TABLE>

<P>After you type the more and page programs' commands, you need not press the Enter or Return key (except, of course, in the case of the <I>n</I>Return command). The programs execute the commands immediately after you type them.

<BR></P>

<P>You can invoke more(page) with certain options that specify the program's behavior. For example, these programs can display explicit error messages instead of just beeping. Table 6.2 lists the most commonly used options for more and page.

<BR></P>

<UL>

<LH><B>Table 6.2. Options for </B><B>more</B><B> and </B><B>page</B>

<BR></LH></UL>

<TABLE BORDER>

<TR>

<TD>

<PRE><I>Option</I>

<BR></PRE>

<TD>

<PRE><I>Meaning</I>

<BR></PRE>

<TR>

<TD>

<P>-c</P>

<TD>

<P>Clear before displaying. To display screens more quickly, this option redraws the screen instead of scrolling. You need not use this option with page.</P>

<TR>

<TD>

<P>-d</P>

<TD>

<P>Display an error message instead of beeping if an unrecognized command is typed.</P>

<TR>

<TD>

<P>-r</P>

<TD>

<P>Display each control character as a two-character pattern consisting of a caret followed by the specified character, as in ^C.</P>

<TR>

<TD>

<P>-s</P>

<TD>

<P>Replace any number of consecutive blank lines with a single blank line.</P>

<TR>

<TD>

<P>-lines</P>

<TD>

<P>Make lines the number of lines in a screenfull.</P>

<TR>

<TD>

<P>+n</P>

<TD>

<P>Start at line number n.</P>

<TR>

<TD>

<P>+/pattern</P>

<TD>

<P>Start two lines above the line that contains the regular expression pattern. (Regular expressions are explained in the next section.)</P></TABLE>

<P>The more(page) program is a legacy from the Berkeley version of UNIX. System V variants give us pg, another screen-at-a-time file viewer. The pg program offers a little more versatility by giving you more control over your movement within a file (you 
can move both forward and backward) and your search for patterns. The program has its own commands and a set of command-line options. Table 6.3 lists the more frequently used commands. Unlike more and page, the pg program requires that you always press the 

Return or Enter key to execute its commands.

<BR></P>

<PRE>$pg [options] file</PRE>

<UL>

<LH><B>Table 6.3. Commands for </B><B>pg</B>

<BR></LH></UL>

<TABLE BORDER>

<TR>

<TD>

<PRE><I>Command</I>

<BR></PRE>

<TD>

<PRE><I>Meaning</I>

<BR></PRE>

<TR>

<TD>

<P><I>n</I>Return</P>

<TD>

<P>If no <I>n</I> value is entered or if a value of +1 is entered, display the next page. If the value of n is &#151;1, display the previous page. If the value of <I>n</I> has no sign, display page number n. For example, a value of 3 causes pg to display 
page 3. (Depending on your keyboard, you can press either the Return or Enter key.)</P>

<TR>

<TD>

<P><I>n</I>d, ^D</P>

<TD>

<P>Scroll half a screen. The value <I>n</I> can be positive or negative. So, for example, 2d will scroll full screen forward, and -3d will scroll one and a half screens back.</P>

<TR>

<TD>

<P>nz</P>

<TD>

<P>Same as nReturn except that if an n value is entered, it becomes the number of lines per screenfull.</P>

<TR>

<TD>

<P>., ^L</P>

<TD>

<P>Redisplay (clear the screen and then display again) the current page of text.</P>

<TR>

<TD>

<P>$</P>

<TD>

<P>Displays the last screenfull in the file.</P>

<TR>

<TD>

<P>n/pattern/</P>

<TD>

<P>Search forward for the nth occurrence of pattern. (The default value for n is 1.) Searching begins immediately after the current page and continues to the end of the current file, without wrap-around.</P>

<TR>

<TD>

<P>n?pattern?</P>

<TD>

<P>Search backward for the nth occurrence of pattern. (The default value for n is 1.) Searching begins immediately before the current page and continues to the beginning of the current file, without wrap-around.</P>

<TR>

<TD>

<P>h</P>

<TD>

<P>Display an abbreviated summary of available commands.</P>

<TR>

<TD>

<P>q, Q</P>

<TD>

<P>Quit pg.</P>

<TR>

<TD>

<P>!command</P>

<TD>

<P>Execute the shell command command as if it were typed on a command line.</P></TABLE>

<P>Addressing is the ability to specify a number with a sign or a number without a sign. A number with no sign provides absolute addressing; for example, pressing 3 followed by the Return key displays page 3. A number with a sign provides relative 
addressing; that is, the command moves you to a line relative to the current line.

<BR></P>

<P>The pg program has several startup options that modify its behavior. Table 6.4 describes the most frequently used options.

<BR></P>

<UL>

<LH><B>Table 6.4. Some of </B><B>pg</B><B>'s Startup Options</B>

<BR></LH></UL>

<TABLE BORDER>

<TR>

<TD>

<PRE><I>Options</I>

<BR></PRE>

<TD>

<PRE><I>Meanings</I>

<BR></PRE>

<TR>

<TD>

<P>-<I>n</I></P>

<TD>

<P>Change the number of lines per page to the value of <I>n</I>. Otherwise, the number of lines is determined automatically by the terminal. For example, a 24-line terminal automatically uses 23 lines per page.</P>

<TR>

<TD>

<P>-c</P>

<TD>

<P>Clear the screen before displaying a page.</P>

<TR>

<TD>

<P>-n</P>

<TD>

<P>Remove the requirement that you press Return or Enter after you type the command. Note: Some commands will still require that you press Enter or Return.</P>

<TR>

<TD>

<P>-p string</P>

<TD>

<P>Change the prompt from a colon (:) to string. If string contains the two characters %d, they are replaced by the current page number when the prompt appears.</P>

<TR>

<TD>

<P>-r</P>

<TD>

<P>Prevent the use of !command and display an error message if the user attempts to use it.</P>

<TR>

<TD>

<P>-s</P>

<TD>

<P>Print all messages and prompts in standout mode (which is usually inverse video).</P>

<TR>

<TD>

<P>+<I>n</I></P>

<TD>

<P>Start the display at line number <I>n</I>.</P>

<TR>

<TD>

<P>+/pattern/</P>

<TD>

<P>Start the display at the first line that contains the regular expression pattern. Regular expressions are explained in the next section.</P></TABLE>

<P>Each of the commands discussed in this section can accept a list of file names on the command line, and display the next file when it reaches the end of the current file. 

<BR></P>

<H3 ALIGN="CENTER">

<CENTER><A ID="I6" NAME="I6">

<FONT SIZE=4><B>Searching for Stringswith the </B><B><I>grep</I></B><B> Family</B>

<BR></FONT></A></CENTER></H3>

<P>Suppose that you want to know whether a certain person has an account on your system. You can use more, page, or pg to browse through /etc/passwd looking for that person's name, but if your system has many users, that can take a long time. Besides, an 
easier way is available: grep. It searches one or more files for the pattern of the characters that you specify and displays every line in the file or files that has that pattern in it.

<BR></P>

<P>grep stands for global/regular expression/print; that is, search through an entire file (do a global search) for a specified regular expression (the pattern that you specified) and display the line or lines that contain the pattern.

<BR></P>

<P>Before you can use grep and the other members of the grep family, you must explore regular expressions, which are what gives the grep commands (and many other UNIX commands) their power. After that, you will learn all of the details of the grep family 
of commands.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I7" NAME="I7">

<FONT SIZE=3><B>Regular Expressions</B>

<BR></FONT></A></CENTER></H4>

<P>A regular expression is a sequence of ordinary characters and special operators. Ordinary characters include the set of all uppercase and lowercase letters, digits, and other commonly used characters: the tilde (~), the back quotation mark ('), the 
exclamation mark (!), the &quot;at&quot; sign (@), the pound sign (#), the underscore (_), the hyphen (-), the equals sign (=), the colon (:), the semicolon (;), the comma (,), and the slash (/). The special operators are backslash (\), dot (.), asterisk 
(*), left square bracket ([), caret (^), dollar sign ($), right square bracket (]).

<BR></P>

<P>By using regular expressions, you can search for general strings in a file. For example, you can tell grep to show you all lines in a file that contain any of the following:  the word Unix, the word UNIX, a pattern consisting of four digits, a ZIP code, 

a name, nothing, or all the vowels in alphabetic order.

<BR></P>

<P>You can also combine two strings into a pattern. For example, to combine a search for Unix and UNIX, you can specify a word that begins with U, followed by n or N, followed by i or I, and ending with x or X.

<BR></P>

<P>Several UNIX commands use regular expressions to find text in files. Usually you supply a regular expression to a command to tell that command what to search for. Most regular expressions match more than one text string.

<BR></P>

<P>There are two kinds of regular expressions: limited and full (sometimes called extended). Limited regular expressions are a subset of full regular expressions, but UNIX commands are inconsistent in the extended operations that they permit. At the end of 

this discussion, you'll find a table that lists the most common commands in UNIX System V Release 4 that use regular expressions, along with the operations that they can perform.

<BR></P>

<P>The simplest form of a regular expression includes only ordinary characters, and is called a string. The grep family (grep, egrep, and fgrep) matches a string wherever it finds the regular expression, even if it's surrounded by other characters. For 
example, the is a regular expression that matches only the three-letter sequence t, h, and e. This string is found in the words the, therefore, bother, and many others.

<BR></P>

<P>Two of the members of the grep family use regular expressions&#151;the third, fgrep, operates only on strings:

<BR></P>

<TABLE BORDER>

<TR>

<TD>

<P>grep</P>

<TD>

<P>The name means to search globally (throughout the entire file) for a regular expression and print the line that contains it. In its simplest form, grep is called as follows:</P>

<TR>

<TD>

<P><BR>]</P>

<TD>

<P>grep regular_expression filename</P>

<TR>

<TD>

<P><BR>]</P>

<TD>

<P>When grep finds a match of regular_expression, it displays the line of the file that contains it and then continues searching for a subsequent match. Thus, grep displays every line of a file that contains a text string that matches the regular 
expression.</P>

<TR>

<TD>

<P>egrep</P>

<TD>

<P>You call this member exactly the same way as you call grep. However, this member uses an extended set of regular expression operators, that will be explained later, after you master the usual set.</P></TABLE>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="caution.gif" WIDTH = 37 HEIGHT = 35><B>CAUTION:</B> None of these commands alter the original file. Output goes to stdout (by default, stdout is the screen). To save the results, you must redirect the output to a file.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>The contents of the following file are used in subsequent sections to demonstrate how you can use the grep family to search for regular expressions:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -