rhl26.htm

来自「linux的初学电子书」· HTM 代码 · 共 2,804 行 · 第 1/5 页

HTM
2,804 行
字号

<BR>

<P ALIGN=CENTER>

<CENTER>

<FONT COLOR="#000080"><B>Table 26.6. Metacharacters.</B></FONT></CENTER>

<BR>



<TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Metacharacter</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Meaning</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Example</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Meaning of </I><I>Example</I>

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

~

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

The beginning of the field

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

$3 ~ /^b/

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Matches if the third field starts with b

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

$

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

The end of the field

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

$3 ~ /b$/

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Matches if the third field ends with b

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Matches any single character

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

$3 ~ /i.m/

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Matches any record that has a third field value of i, another character, and then m

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

|

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Or

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

/cat|CAT/

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Matches cat or CAT

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

*

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Zero or more repetitions of a character

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

/UNI*X/

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Matches UNX, UNIX, UNIIX, UNIIIX, and so on

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

+

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

One or more repetitions of a character

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

/UNI+X/

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Matches UNIX, UNIIX, and so on, but not UNX

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

\{a,b\}

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

The number of repetitions between a and b (both integers)

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

/UNI\{1,3\}X

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Matches only UNIX, UNIIX, and UNIIIX

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

?

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Zero or one repetitions of a string

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

/UNI?X/

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Matches UNX and UNIX only

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

[]

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Range of characters

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

/I[BDG]M/

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Matches IBM, IDM, and IGM

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

[^]

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Not in the set

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

/I[^DE]M/

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Matches all three character sets starting with I and ending in M, except IDM and IEM</FONT>

</TABLE><P>Some of these metacharacters are used frequently. You will see some examples later in this chapter.

<BR>

<BR>

<A NAME="E68E218"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Calling </B><B>gawk</B><B> Programs</B></FONT></CENTER></H3>

<BR>

<P>Running pattern-action pairs one or two at a time from the command line would be pretty difficult (and time consuming), so gawk allows you to store pattern-action pairs in a file. A gawk program (called a script) is a set of pattern-action pairs stored 
in an ASCII file. For example, this could be the contents of a valid gawk script:

<BR>

<PRE>

<FONT COLOR="#000080">/tparker/{print $6}

$2 != &quot;foo&quot; {print}</FONT></PRE>

<P>The first line would look for tparker and print the sixth column, and the second line would start at the top of the file again and look for second columns that don't match the string &quot;foo&quot;, then display the entire line. When you are writing a 
script, you don't need to worry about the quotation marks around the pattern-action pairs as you did on the command line, because the new command to execute this script makes it obvious where the pattern-action pairs start and end.

<BR>

<P>After you have saved all of the pattern-action pairs in a program, they are called by gawk with the -f option on the command line:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">gawk -f script filename</FONT></PRE>

<P>This command causes gawk to read all of the pattern-action pairs from the file script and process them against the file called filename. This is how most gawk programs are written. Don't confuse the -f and -F options!

<BR>

<P>If you want to specify a different field separator on the command line (they can be specified in the script, but use a special format you'll see later), the -F option must follow the -f option:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">gawk -f script -F&quot;:&quot; filename</FONT></PRE>

<P>If you want to process more than one file using the script, just append the names of the files:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">gawk -f script filename1 filename2 filename3 ...</FONT></PRE>

<P>By default, all output from the gawk command is displayed on the screen. You could redirect it to a file with the usual Linux redirection commands:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">gawk -f script filename &gt; save_file</FONT></PRE>

<P>There is another way of specifying the output file from within the script, but we'll come back to that in a moment.

<BR>

<BR>

<A NAME="E69E324"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>BEGIN and END</B></FONT></CENTER></H4>

<BR>

<P>Two special patterns supported by gawk are useful when writing scripts. The BEGIN pattern is used to indicate any actions that should take place before gawk starts processing a file. This is usually used to initialize values, set parameters such as 
field separators, and so on. The END pattern is used to execute any instructions after the file has been completely processed. Typically, this can be for summaries or completion notices.

<BR>

<P>Any instructions following the BEGIN and END patterns are enclosed in curly braces to identify which instructions are part of both patterns. Both BEGIN and END must appear in capitals. Here's a simple example of a gawk script that uses BEGIN and END, 
albeit only for sending a message to the terminal:

<BR>

<PRE>

<FONT COLOR="#000080">BEGIN { print &quot;Starting the process the file&quot; }

$1 == &quot;UNIX&quot; {print}

$2 &gt; 10 {printf &quot;This line has a value of %d&quot;, $2}

END { print &quot;Finished processing the file. Bye!&quot;}</FONT></PRE>

<P>In this script, a message is initially printed out, and each line that has the word UNIX in the first column is echoed to the screen. Next, the file is processed again to look for any line with the second column greater than 10, and the message is 
generated with its current value. Finally, the END pattern prints out a message that the program is finished.

<BR>

<BR>

<A NAME="E69E325"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Variables</B></FONT></CENTER></H4>

<BR>

<P>If you have used any programming language before, you know that a variable is a storage location for a value. Each variable has a name and an associated value, which may change.

<BR>

<P>With gawk, you assign a variable a value using the assignment operator, =:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">var1 = 10</FONT></PRE>

<P>This assigns the value 10 (numeric, not string) to the variable var1. With gawk, you don't have to declare variable types before you use them as you must with most other languages. This makes it easy to work with variables in gawk.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>Don't confuse the assignment operator, =, which assigns a value, with the comparison operator, ==, which compares two values. This is a common error that takes a little practice to overcome.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>The gawk language lets you use variables within actions, so the pattern-action pair

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">$1 == &quot;Plastic&quot; { count = count + 1 }</FONT></PRE>

<P>checks to see if the first column is equal to the string &quot;Plastic&quot;, and if it is, increments the value of count by one. Somewhere above this line we should set a preliminary value for the variable count (usually in the BEGIN section), or we 
will be adding one to something that isn't a recognizable number.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>Actually, gawk assigns all variables a value of zero when they are first used, so you don't really have to define the value before you use it. It is, however, good programming practice to initialize the variable anyway.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>Here's a more complete example:

<BR>

<PRE>

<FONT COLOR="#000080">BEGIN { count = 0 }

$5 == &quot;UNIX&quot; { count = count + 1 }

END { printf &quot;%d occurrences of UNIX were found&quot;, count }</FONT></PRE>

<P>In the BEGIN section, the variable count is set to zero. Then, the gawk pattern-action pair is processed, with every occurrence of &quot;UNIX&quot; adding one to the value of count. After the entire file has been processed, the END statement displays 
the total number.

<BR>

<P>Variables can be used in combination with columns and values, so all of the following statements are legal:

<BR>

<PRE>

<FONT COLOR="#000080">count = count + $6

count = $5 - 8

count = $5 + var1</FONT></PRE>

<P>Variables can also be part of a pattern. The following are all valid as pattern-action pairs:

<BR>

<PRE>

<FONT COLOR="#000080">$2 &gt; max_value {print &quot;Max value exceeded by &quot;, $2 - max_value}

$4 - var1 &lt; min_value {print &quot;Illegal value of &quot;, $4}</FONT></PRE>

<P>Two special operators are used with variables to increment and decrement by one, because these are common operations. Both of these special operators are borrowed from the C language:

<BR>



<TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

count++

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

increments count by one

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

count&#151;

</FONT>

⌨️ 快捷键说明

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