rhl26.htm
来自「linux的初学电子书」· HTM 代码 · 共 2,804 行 · 第 1/5 页
HTM
2,804 行
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Description</I>
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
==
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Equal to
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
!=
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Not equal to
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
>
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Greater than
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Less than
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
>=
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Greater than or equal to
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<=
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Less than or equal to</FONT>
</TABLE><P>These are probably familiar to you from arithmetic and other programming languages you may have seen. From this, you can surmise that the command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">gawk '$4 > 100' testfile</FONT></PRE>
<P>will display every line in testfile in which the value in the fourth column is greater than 100.
<BR>
<P>All of the normal arithmetic commands are available, including add, subtract, multiply, and divide. There are also more advanced functions such as exponentials and remainders (also called modulus). Table 26.2 shows the basic arithmetic operations that
gawk supports.
<BR>
<BR>
<P ALIGN=CENTER>
<CENTER>
<FONT COLOR="#000080"><B>Table 26.2. Basic arithmetic operators.</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>Operator</I>
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Description</I>
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Example</I>
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
+
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Addition
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
2+6
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Subtraction
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
6-3
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
*
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Multiplication
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
2*5
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
/
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Division
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
8/4
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
^
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Exponentiation
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
3^2 (=9)
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
%
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Remainder
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
9%4 (=1)</FONT>
</TABLE><P>You can combine column numbers and math, too. For example, the action
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">{print $3/2}</FONT></PRE>
<P>divides the number in the third column by 2.
<BR>
<P>There is also a set of arithmetic functions for trigonometry and generating random numbers. See Table 26.3.
<BR>
<BR>
<P ALIGN=CENTER>
<CENTER>
<FONT COLOR="#000080"><B>Table 26.3. Random-number and trigonometric functions.</B></FONT></CENTER>
<BR>
<TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Function
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Description
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
sqrt(x)
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Square root of x
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
sin(x)
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Sine of x (in radians)
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
cos(x)
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Cosine of x (in radians)
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
atan2(x,y)
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Arctangent of x/y
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
log(x)
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Natural logarithm of x
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
exp(x)
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
The constant e to the power x
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
int(x)
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Integer part of x
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
rand()
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Random number between 0 and 1
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
srand(x)
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Set x as seed for rand()</FONT>
</TABLE><P>The order of operations is important to gawk, as it is to regular arithmetic. The rules gawk follows are the same as with arithmetic: all multiplications, divisions, and remainders are performed before additions and subtractions. For example,
the command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">{print $1+$2*$3}</FONT></PRE>
<P>multiplies column two by column three and then adds the result to column one. If you wanted to force the addition first, you would have to use parentheses:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">{print ($1+$2)*$3}</FONT></PRE>
<P>Because these are the same rules you have heard about since grade school, they shouldn't cause you any confusion. Remember, if in doubt, put parentheses in the proper places to force the operations.
<BR>
<BR>
<A NAME="E69E320"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Strings and Numbers</B></FONT></CENTER></H4>
<BR>
<P>If you've used any other programming language, these concepts will be familiar to you. If you are new to programming, you will probably find them obvious, but you'd be surprised how many people get things hopelessly muddled by using strings when they
should have used numbers.
<BR>
<P>A string is a set of characters that are to be interpreted literally by gawk. Strings are surrounded by quotation marks. Numbers are not surrounded by quotation marks and are treated as real values.
<BR>
<P>For example, the command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">gawk '$1 != "Tim" {print}' testfile</FONT></PRE>
<P>will print any line in testfile that doesn't have the word Tim in the first column. If we had left out the quotation marks around Tim, gawk wouldn't have processed the command properly. The command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">gawk '$1 == "50" {print}' testfile</FONT></PRE>
<P>will display any line that has the string 50 in it. It does not attempt to see if the value stored in the first column is different than 50; it just does a character check. The string 50 is not equal to the number 50 as far as gawk is concerned.
<BR>
<BR>
<A NAME="E69E321"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Formatting Output</B></FONT></CENTER></H4>
<BR>
<P>We've seen how to do simple actions in the commands we've already discussed, but you can do several things in an action. For example, the command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">gawk '$1 != "Tim" {print $1, $5, $6, $2}' testfile</FONT></PRE>
<P>will print the first, fifth, sixth, and second columns of testfile for every line that doesn't have the first column equal to "Tim". You can place as many of these columns as you want in a print
<BR>command.
<BR>
<P>Indeed, you can place strings in a print command, too, such as in this command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">gawk '$1 != "Tim" {print "The entry for ", $1, "is not Tim. ", $2}' testfile</FONT></PRE>
<P>which will print the strings and the columns as shown. Each section of the print command is separated by a comma. There are also spaces at the end of the strings to ensure there is a space between the string and the value of the column that is printed.
<BR>
<P>You can use additional formatting instructions to make gawk format the output properly. These instructions are borrowed from the C language, and they use the command printf (print formatted) instead of print.
<BR>
<P>The printf command uses a placeholder scheme, but the gawk language knows how to format the entry because of the placeholder and looks later in the command line to find out what to put there. An example will help make this obvious:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">{printf "%5s likes this language\n", $2}</FONT></PRE>
<P>The %5s part of the line instructs gawk how to format the string, in this case using five string characters. The value to place in this position is given at the end of the line as the second column. The \n at the end of the quoted section is a newline
character. If the second column of a four-line file held names, printf would format the output like this:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?