444-447.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 214 行
HTML
214 行
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:gawk</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=25//-->
<!--PAGES=444-447//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="442-444.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="447-450.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Simple Patterns</FONT></H4>
<P>As you might have figured out, <TT>gawk</TT> numbers all of the fields in a record. The first field is <TT>$1</TT>, the second is <TT>$2</TT>, and so on. The entire record is called <TT>$0</TT>. As a short form, <TT>gawk</TT> allows you to ignore the <TT>$0</TT> in simple commands, so each of the following instructions results in the same output (the latter one because no action causes the entire line to be printed):</P>
<!-- CODE SNIP //-->
<PRE>
gawk ’/tparker/{print $0}’ /etc/passwd
gawk ’/tparker/{print}’ /etc/passwd
gawk ’/tparker/’ /etcpasswd
</PRE>
<!-- END CODE SNIP //-->
<P>Suppose you want to do more than match a simple character string. The <TT>gawk</TT> language has many powerful features, but we’ll introduce just a few. We can, for example, make a comparison of a field with a value:</P>
<!-- CODE SNIP //-->
<PRE>
gawk ’$2 == “foo” {print $3}’ testfile
</PRE>
<!-- END CODE SNIP //-->
<P>The preceding command instructs <TT>gawk</TT> to compare the second string (<TT>$2</TT>) of each record in <TT>testfile</TT> and check to see if it is equal to the string <TT>foo</TT>. If it is, <TT>gawk</TT> prints the third column (<TT>$3</TT>).</P>
<P>This command demonstrates a few important points. First, there are no slashes around the pattern because we are not matching a pattern but are evaluating something. Slashes are used only for character matches. Second, the <TT>==</TT> sign means “is equal to.” We must use two equal signs, because the single equal sign is used for assignment of values, as you will see shortly. Finally, we put double quotation marks around <TT>foo</TT> because we want <TT>gawk</TT> to interpret it literally. Only strings of characters that are to be literally interpreted must be quoted in this manner.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>Don’t confuse the quotation marks used for literal characters with those used to surround the pattern-action pair on the command line. If you use the same quotation marks for both, <TT>gawk</TT> is unable to process the command properly.<HR></FONT>
</BLOCKQUOTE>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Comparisons and Arithmetic</FONT></H4>
<P>An essential component of any programming language is the ability to compare two strings or numbers and evaluate whether they are equal or different. The <TT>gawk</TT> program has several comparisons, including <TT>==</TT>, which you just saw in an example. Table 25.1 shows the important comparisons.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 25.1.</B> The important comparisons.
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="35%" ALIGN="LEFT">Comparison
<TH WIDTH="65%" ALIGN="LEFT">Description
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD><TT>==</TT>
<TD>Equal to
<TR>
<TD><TT>!=</TT>
<TD>Not equal to
<TR>
<TD><TT>></TT>
<TD>Greater than
<TR>
<TD><TT><</TT>
<TD>Less than
<TR>
<TD><TT>>=</TT>
<TD>Greater than or equal to
<TR>
<TD><TT><=</TT>
<TD>Less than or equal to
<TR>
<TD COLSPAN="2"><HR>
</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 following command will display every line in <TT>testfile</TT> in which the value in the fourth column is greater than 100:</P>
<!-- CODE SNIP //-->
<PRE>
gawk ’$4 > 100’ testfile
</PRE>
<!-- END CODE SNIP //-->
<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 <I>moduli</I>). Table 25.2 shows the basic arithmetic operations that <TT>gawk</TT> supports.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 25.2.</B> Basic arithmetic operators.
<TR>
<TH COLSPAN="3"><HR>
<TR>
<TH WIDTH="20%" ALIGN="LEFT">Operator
<TH WIDTH="40%" ALIGN="LEFT">Description
<TH WIDTH="40%" ALIGN="LEFT">Example
<TR>
<TH COLSPAN="3"><HR>
<TR>
<TD><TT>+</TT>
<TD>Addition
<TD>2+6
<TR>
<TD><TT>-</TT>
<TD>Subtraction
<TD>6-3
<TR>
<TD><TT>*</TT>
<TD>Multiplication
<TD>2*5
<TR>
<TD><TT>/</TT>
<TD>Division
<TD>8/4
<TR>
<TD><TT>^</TT>
<TD>Exponentiation
<TD>3^2 (=9)
<TR>
<TD><TT>%</TT>
<TD>Remainder
<TD>9%4 (=1)
<TR>
<TD COLSPAN="3"><HR>
</TABLE>
<P>You can combine column numbers and math, too:
</P>
<!-- CODE SNIP //-->
<PRE>
{print $3/2}
</PRE>
<!-- END CODE SNIP //-->
<P>This action divides the number in the third column by 2.
</P>
<P>There is also a set of arithmetic functions for trigonometry and generating random numbers (see Table 25.3).</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 25.3.</B> Random-number and trigonometric functions.
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="35%" ALIGN="LEFT">Function
<TH WIDTH="65%" ALIGN="LEFT">Description
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD><TT>sqrt(<I>x</I>)</TT>
<TD>Square root of <I>x</I>
<TR>
<TD><TT>sin(<I>x</I>)</TT>
<TD>Sine of <I>x</I> (in radians)
<TR>
<TD><TT>cos(<I>x</I>)</TT>
<TD>Cosine of <I>x</I> (in radians)
<TR>
<TD><TT>atan2(<I>x,y</I>)</TT>
<TD>Arctangent of <I>x/y</I>
<TR>
<TD><TT>log(<I>x</I>)</TT>
<TD>Natural logarithm of <I>x</I>
<TR>
<TD><TT>exp(<I>x</I>)</TT>
<TD>The constant e to the power <I>x</I>
<TR>
<TD><TT>int(<I>x</I>)</TT>
<TD>Integer part of <I>x</I>
<TR>
<TD><TT>rand()</TT>
<TD>Random number between 0 and 1
<TR>
<TD><TT>srand(<I>x</I>)</TT>
<TD>Set <I>x</I> as seed for <TT>rand()</TT>
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>The order of operations is important to <TT>gawk</TT>, as it is to regular arithmetic. The rules <TT>gawk</TT> follows are the same as with arithmetic: All multiplications, divisions, and remainders are performed before additions and subtractions:</P>
<!-- CODE SNIP //-->
<PRE>
{print $1+$2*$3}
</PRE>
<!-- END CODE SNIP //-->
<P>The preceding command multiplies column two by column three and then adds the result to column one. If you wanted to force the addition first, use parentheses:
</P>
<!-- CODE SNIP //-->
<PRE>
{print ($1+$2)*$3}
</PRE>
<!-- END CODE SNIP //-->
<P>Because these are the same rules you have known about since grade school, they should not cause you any confusion. Remember, if in doubt, put parentheses in the proper places to force the operations.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="442-444.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="447-450.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?