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

📄 ch26.htm

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<A NAME="Heading7<FONT COLOR="#000077"><B>NOTE: </B></FONT>The quotes around



	the entire pattern-action pair are very important and should not be left off. Without



	them, the command might not execute properly. Make sure the quotes match (don't use



	a single quote at the beginning and a double quote at the end).



<HR>







</DL>







<P>You can combine more than one pattern-action pair in a command. For example,<FONT



COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">gawk `/scandal/{print $1} /rumor/{print $2}' gossip_file



</FONT></PRE>



<P>scans each line of <TT>gossip_file</TT> for the patterns &quot;scandal&quot; and



&quot;rumor.&quot; When a match is found, <TT>gawk</TT> prints the first or second



field, respectively.



<H4 ALIGN="CENTER"><A NAME="Heading8<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 the instructions<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">gawk `/tparker/{print $0}' /etc/passwd



gawk `/tparker/{print}' /etc/passwd



gawk `/tparker/' /etc/passwd



</FONT></PRE>



<P>result in the same output (the latter one because no action causes the entire



line to be printed).</P>



<P>Sometimes you want to do more than match a simple character string. The <TT>gawk</TT>



language has many powerful features, but I'll just introduce a few at the moment.



We can, for example, make a comparison of a field with a value. The command<FONT



COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">gawk `$2 == &quot;foo&quot; {print $3}' testfile



</FONT></PRE>



<P>instructs <TT>gawk</TT> to compare the second field (<TT>$2</TT>) of each record



in <TT>testfile</TT> and check to see whether it is equal to the string <TT>foo</TT>.



If it is, <TT>gawk</TT> prints the third field (<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 &quot;is



equal to.&quot; 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 quotations



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.







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading9<FONT COLOR="#000077"><B>NOTE:</B> </FONT>Don't confuse the



	quotes used for literal characters with those used to surround the pattern-action



	pair on the command line. If you use the same quote marks for both, <TT>gawk</TT>



	will be unable to process the command properly.



<HR>







</DL>







<H4 ALIGN="CENTER"><A NAME="Heading10<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 26.1 shows the important comparisons. <BR>







<CENTER>



<P><FONT SIZE="4"><B>Table 26.1. The important comparisons. </B></FONT>



<TABLE BORDER="0">



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="82" ALIGN="LEFT"><I>Comparison</I></TD>



		<TD ALIGN="LEFT"><I>Description</I></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="82" ALIGN="LEFT"><TT>==</TT></TD>



		<TD ALIGN="LEFT">Equal to</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="82" ALIGN="LEFT"><TT>!=</TT></TD>



		<TD ALIGN="LEFT">Not equal to</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="82" ALIGN="LEFT"><TT>&gt;</TT></TD>



		<TD ALIGN="LEFT">Greater than</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="82" ALIGN="LEFT"><TT>&lt;</TT></TD>



		<TD ALIGN="LEFT">Less than</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="82" ALIGN="LEFT"><TT>&gt;=</TT></TD>



		<TD ALIGN="LEFT">Greater than or equal to</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="82" ALIGN="LEFT"><TT>&lt;=</TT></TD>



		<TD ALIGN="LEFT">Less than or equal to</TD>



	</TR>



</TABLE>







</CENTER>



<P><BR>



These are probably familiar to you from arithmetic and other programming languages



you may have seen. From this, you can surmise that the command<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">gawk `$4 &gt; 100' testfile



</FONT></PRE>



<P>will display every line in <TT>testfile</TT> in which the value in the fourth



field is greater than 100.</P>



<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 <TT>gawk</TT> supports. <BR>







<CENTER>



<P><FONT SIZE="4"><B>Table 26.2. Basic arithmetic operators. </B></FONT>



<TABLE BORDER="0">



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="67" ALIGN="LEFT"><I>Operator</I></TD>



		<TD WIDTH="88" ALIGN="LEFT"><I>Description</I></TD>



		<TD ALIGN="LEFT"><I>Example</I></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="67" ALIGN="LEFT"><TT>+</TT></TD>



		<TD WIDTH="88" ALIGN="LEFT">Addition</TD>



		<TD ALIGN="LEFT"><TT>2+6</TT></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="67" ALIGN="LEFT"><TT>-</TT></TD>



		<TD WIDTH="88" ALIGN="LEFT">Subtraction</TD>



		<TD ALIGN="LEFT"><TT>6-3</TT></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="67" ALIGN="LEFT"><TT>*</TT></TD>



		<TD WIDTH="88" ALIGN="LEFT">Multiplication</TD>



		<TD ALIGN="LEFT"><TT>2*5</TT></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="67" ALIGN="LEFT"><TT>/</TT></TD>



		<TD WIDTH="88" ALIGN="LEFT">Division</TD>



		<TD ALIGN="LEFT"><TT>8/4</TT></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="67" ALIGN="LEFT"><TT>^</TT></TD>



		<TD WIDTH="88" ALIGN="LEFT">Exponentiation</TD>



		<TD ALIGN="LEFT"><TT>3^2</TT> (=9)</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="67" ALIGN="LEFT"><TT>%</TT></TD>



		<TD WIDTH="88" ALIGN="LEFT">Remainder</TD>



		<TD ALIGN="LEFT"><TT>9%4</TT> (=1)</TD>



	</TR>



</TABLE>







</CENTER>



<P><BR>



You can combine fields and math, too. For example, the action<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">{print $3/2}



</FONT></PRE>



<P>divides the number in the third field by 2.</P>



<P>There is also a set of arithmetic functions for trigonometry and generating random



numbers. See Table 26.3. <BR>







<CENTER>



<P><FONT SIZE="4"><B>Table 26.3. Random-number and trigonometric functions. </B></FONT>



<TABLE BORDER="0">



	<TR ALIGN="LEFT" rowspan="1">



		<TD HEIGHT="14" ALIGN="LEFT"><I>Function</I></TD>



		<TD HEIGHT="14" ALIGN="LEFT"><I>Description</I></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>sqrt(</TT>x<TT>)</TT></TD>



		<TD ALIGN="LEFT">Square root of x</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>sin(</TT>x<TT>)</TT></TD>



		<TD ALIGN="LEFT">Sine of x (in radians)</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>cos(</TT>x<TT>)</TT></TD>



		<TD ALIGN="LEFT">Cosine of x (in radians)</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>atan2(</TT>x<TT>,</TT>y<TT>)</TT></TD>



		<TD ALIGN="LEFT">Arctangent of x/y</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>log(</TT>x<TT>)</TT></TD>



		<TD ALIGN="LEFT">Natural logarithm of x</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>exp(</TT>x<TT>)</TT></TD>



		<TD ALIGN="LEFT">The constant e to the power x</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>int(</TT>x<TT>)</TT></TD>



		<TD ALIGN="LEFT">Integer part of x</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>rand()</TT></TD>



		<TD ALIGN="LEFT">Random number between 0 and 1</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>srand(</TT>x<TT>)</TT></TD>



		<TD ALIGN="LEFT">Set x as seed for <TT>rand()</TT></TD>



	</TR>



</TABLE>







</CENTER>



<P><BR>



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. For example,



the command<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">{print $1+$2*$3}



</FONT></PRE>



<P>multiplies field two by field three and then adds the result to field one. If



you wanted to force the addition first, you would have to use parentheses:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">{print ($1+$2)*$3}



</FONT></PRE>



<P>Because these are the same rules you used in algebra, they shouldn't cause you



any confusion. Remember, if in doubt, put parentheses in the proper places to force



the operations.



<CENTER>



<H4><A NAME="Heading11<FONT COLOR="#000077">Strings and Numbers</FONT></H4>



</CENTER>



<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.</P>



<P>A string is a set of characters to be interpreted literally by <TT>gawk</TT>.



Strings are surrounded by quotation marks. Numbers are not surrounded by quotation



marks and are treated as real values.</P>



<P>For example, the command<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">gawk `$1 != &quot;Tim&quot; {print}' testfile



</FONT></PRE>



<P>will print any line in <TT>testfile</TT> that doesn't have the word <TT>Tim</TT>



in the first field. If we had left out the quotation marks around <TT>Tim</TT>, <TT>gawk</TT>



wouldn't have processed the command properly. The command</P>



<PRE><FONT COLOR="#0066FF">gawk `$1 == &quot;50&quot; {print}' testfile



</FONT></PRE>



<P>will display any line that has the string <TT>50</TT> in it. It does not attempt



to see if the value stored in the first field is different than 50; it just does



a character check. The string <TT>50</TT> is not equal to the number 50 as far as



<TT>gawk</TT> is concerned.



<CENTER>



<H4><A NAME="Heading12<FONT COLOR="#000077">Formatting Output</FONT></H4>



</CENTER>



<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<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">gawk `$1 != &quot;Tim&quot; {print $1, $5, $6, $2}' testfile



</FONT></PRE>



<P>will print the first, fifth, sixth, and second field of <TT>testfile</TT> for



every line that doesn't have the first field equal to <TT>&quot;Tim&quot;</TT>. You



can place as many of these fields as you want in a print command.</P>



<P>Indeed, you can place strings in a print command, too, such as in the command<FONT



COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">gawk `$1 != &quot;Tim&quot; {print &quot;The entry for &quot;, $1, &quot;is not Tim. &quot;, $2}' testfile



</FONT></PRE>



<P>which will print the strings and the fields 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 field that is



printed.</P>



<P>You can use additional formatting instructions to make <TT>gawk</TT> format the

⌨️ 快捷键说明

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