459-461.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 124 行

HTML
124
字号
<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=459-461//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="456-459.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="../ch26/463-466.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">The for Loop</FONT></H4>

<P>The <TT>for</TT> loop is commonly used when you want to initialize a value and then ignore it. The syntax of the <TT>gawk for</TT> loop is</P>

<!-- CODE SNIP //-->

<PRE>

for (<I>initialization</I>; <I>expression</I>; <I>increment</I>) &#123;

 <I>command</I>

   &#125;

</PRE>

<!-- END CODE SNIP //-->

<P>The initialization is executed only once and then ignored, the expression is evaluated each time the loop executes, and the increment is executed each time the loop is executed. Usually the increment is a counter of some type, but it can be any collection of valid commands. Here&#146;s an example of a <TT>for</TT> loop, which is the same basic program as shown for the <TT>while</TT> loop:</P>

<!-- CODE SNIP //-->

<PRE>

# interest calculation computes compound interest

# inputs from a file are the amount, interest_rate, and years

&#123;for (var=1; var &lt;= &#36;3; var&#43;&#43;) &#123;

   printf(&#147;%f\n&#148;, &#36;1*(1&#43;&#36;2)^var)

   &#125;

&#125;

</PRE>

<!-- END CODE SNIP //-->

<P>In this case, <TT>var</TT> is initialized when the <TT>for</TT> loop starts. The expression is evaluated, and if true, the loop runs. Then the value of <TT>var</TT> is incremented and the expression is tested again.</P>

<P>The format of the <TT>for</TT> loop may look strange if you haven&#146;t encountered programming languages before, but it is the same as the <TT>for</TT> loop used in C, for example.</P>

<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">next and exit</FONT></H4>

<P>The <TT>next</TT> instruction tells <TT>gawk</TT> to process the next record in the file, regardless of what it is doing. For example, consider this script:</P>

<!-- CODE SNIP //-->

<PRE>

&#123; <I>command1</I>

     <I>command2</I>

     <I>command3</I>

       next

     <I>command4</I>

&#125;

</PRE>

<!-- END CODE SNIP //-->

<P>As soon as the <TT>next</TT> statement is read, <TT>gawk</TT> moves to the next record in the file and starts at the top of the current script block (given by the curly brace). In this example, <I>command4</I> will never be executed because the <TT>next</TT> statement moves back up to <I>command1</I> each time.</P>

<P>The <TT>next</TT> statement is usually used inside an <TT>if</TT> loop, where you may want execution to return to the start of the script if some condition is met.</P>

<P>The <TT>exit</TT> statement makes <TT>gawk</TT> behave as though it has reached the end of the file, and it then executes any <TT>END</TT> patterns (if any exist). This is a useful method of aborting processing if there is an error in the file.</P>

<H4 ALIGN="LEFT"><A NAME="Heading20"></A><FONT COLOR="#000077">Arrays</FONT></H4>

<P>The <TT>gawk</TT> language supports arrays and enables you to access any element in the array easily. No special initialization is necessary with an array, because <TT>gawk</TT> treats it like any other variable. The general format for declaring arrays is as follows:</P>

<!-- CODE SNIP //-->

<PRE>

var[<I>num</I>]=<I>value</I>

</PRE>

<!-- END CODE SNIP //-->

<P>As an example, consider the following script, which reads an input file and generates an output file with the lines reversed in order:

</P>

<!-- CODE SNIP //-->

<PRE>

# reverse lines in a file

&#123;line[NR] = &#36;0 &#125; # remember each line

END &#123;var=NR       # output lines in reverse order

     while (var &gt; 0)&#123;

     print line[var]

     var--

     &#125;

&#125;

</PRE>

<!-- END CODE SNIP //-->

<P>In this simple program (try and do the same task in any other programming language to see how efficient <TT>gawk</TT> is!), we use the <TT>NR</TT> (number of records) built-in variable. After reading each line into the array line<TT>[]</TT>, we simply start at the last record and print them again, stepping down through the array each time. We don&#146;t have to declare the array or do anything special with it, which is one of the powerful features of <TT>gawk</TT>.</P>

<H3><A NAME="Heading21"></A><FONT COLOR="#000077">Summary</FONT></H3>

<P>We&#146;ve only scratched the surface of <TT>gawk</TT>&#146;s abilities, but you may have noticed that it is a relatively easy language to work with and places no special demands on the programmer. That&#146;s one of the reasons <TT>gawk</TT> is so often used for quick programs. It is ideal, for example, for writing a quick script to count the total size of all the files in a directory. In the C language, this would take many lines, but it can be done in less than a dozen lines in <TT>gawk</TT>.</P>

<P>If you are a system administrator or simply a power user, you will find that <TT>gawk</TT> is a great complement to all the other tools you have available, especially because it can accept input from a pipe or redirection. For more information on <TT>gawk</TT>, check the man pages or one of the few <TT>gawk</TT> books that are available.</P>

<P>See the following chapters for information on programming:</P>

<DL>

<DD>C under Linux is discussed in Chapter 26, &#147;Programming in C.&#148;

<DD>Perl, another handy utility included with Linux, is discussed in Chapter 28, &#147;Perl.&#148;

<DD>Tcl and Tk, yet another programming language with extra features for X and Motif, is discussed in Chapter 29, &#147;Introduction to Tcl and Tk.&#148;

</DL>

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="456-459.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="../ch26/463-466.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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