456-459.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 189 行
HTML
189 行
<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=456-459//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="453-456.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="459-461.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading14"></A><FONT COLOR="#000077">Built-In Variables</FONT></H4>
<P>The <TT>gawk</TT> language has a few built-in variables that are used to represent things such as the total number of records processed. These are useful when you want to get totals. Table 25.7 shows the important built-in variables.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 25.7.</B> The important built-in variables.
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="35%" ALIGN="LEFT">Variable
<TH WIDTH="65%" ALIGN="LEFT">Description
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD><TT>NR</TT>
<TD>The number of records read so far
<TR>
<TD><TT>FNR</TT>
<TD>The number of records read from the current file
<TR>
<TD><TT>FILENAME</TT>
<TD>The name of the input file
<TR>
<TD><TT>FS</TT>
<TD>Field separator (default is whitespace)
<TR>
<TD><TT>RS</TT>
<TD>Record separator (default is newline)
<TR>
<TD><TT>OFMT</TT>
<TD>Output format for numbers (default is <TT>%g</TT>)
<TR>
<TD><TT>OFS</TT>
<TD>Output field separator
<TR>
<TD><TT>ORS</TT>
<TD>Output record separator
<TR>
<TD><TT>NF</TT>
<TD>The number of fields in the current record
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>The <TT>NR</TT> and <TT>FNR</TT> values are the same if you are processing only one file, but if you are doing more than one file, <TT>NR</TT> is a running total of all files, while <TT>FNR</TT> is the total for the current file only.</P>
<P>The <TT>FS</TT> variable is useful because it controls the input file’s field separator. To use the colon for the <TT>/etc/passwd</TT> file, for example, use the following command in the script, usually as part of the <TT>BEGIN</TT> pattern:</P>
<!-- CODE SNIP //-->
<PRE>
FS=”:”
</PRE>
<!-- END CODE SNIP //-->
<P>You can use these built-in variables as you would any other. For example, the following command gives you a way to check the number of fields in the file you are processing and generates an error message if the values are incorrect:
</P>
<!-- CODE SNIP //-->
<PRE>
NF <= 5 {print “Not enough fields in the record”}
</PRE>
<!-- END CODE SNIP //-->
<H3><A NAME="Heading15"></A><FONT COLOR="#000077">Control Structures</FONT></H3>
<P>Enough of the details have been covered to allow us to start doing some real <TT>gawk</TT> programming. Although we have not covered all of <TT>gawk</TT>’s pattern and action considerations, we have seen all the important material. Now we can look at writing control structures.</P>
<P>If you have any programming experience at all or have tried some shell script writing, many of these control structures will appear familiar. If you haven’t done any programming, common sense should help, as <TT>gawk</TT> is cleanly laid out without weird syntax. Follow the examples and try a few test programs of your own.</P>
<P>Incidentally, <TT>gawk</TT> enables you to place comments anywhere in your scripts, as long as the comment starts with a <TT>#</TT> sign. You should use comments to indicate what is going on in your scripts if it is not immediately obvious.</P>
<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">The if Statement</FONT></H4>
<P>The <TT>if</TT> statement is used to allow <TT>gawk</TT> to test some condition and, if it is true, execute a set of commands. The general syntax for the <TT>if</TT> statement is as follows:</P>
<!-- CODE SNIP //-->
<PRE>
if (<I>expression</I>) {<I>commands</I>} else {<I>commands</I>}
</PRE>
<!-- END CODE SNIP //-->
<P>The expression is always evaluated to see if it is true or false. No other value is calculated for the <TT>if</TT> expression. Here’s a simple <TT>if</TT> script:</P>
<!-- CODE SNIP //-->
<PRE>
# a simple if loop
(if ($1 == 0){
print “This cell has a value of zero”
}
else {
printf “The value is %d\n”, $1
})
</PRE>
<!-- END CODE SNIP //-->
<P>Notice that the curly braces were used to lay out the program in a readable manner. Of course, this could all have been entered on one line and <TT>gawk</TT> would have understood it, but writing in a nicely formatted manner makes it easier to understand what is going on and to debug the program if the need arises.</P>
<P>In this simple script, we test the first column to see if the value is zero. If it is, a message to that effect is printed. If not, the <TT>printf</TT> statement prints the value of the column.</P>
<P>The flow of the <TT>if</TT> statement is quite simple to follow. There can be several commands in each part, as long as the curly braces mark the start and end. There is no need to have an <TT>else</TT> section. It can be left out entirely, if desired. For example, this is a complete and valid <TT>gawk</TT> script:</P>
<!-- CODE SNIP //-->
<PRE>
(if ($1 == 0){
print “This cell has a value of zero”
})
</PRE>
<!-- END CODE SNIP //-->
<P>The <TT>gawk</TT> language, to be compatible with other programming languages, allows a special format of the <TT>if</TT> statement when a simple comparison is being conducted. This quick-and-dirty <TT>if</TT> structure is harder to read for novices, and I don’t recommend it if you are new to the language. For example, here’s the <TT>if</TT> statement written the proper way:</P>
<!-- CODE SNIP //-->
<PRE>
# a nicely formatted if loop
(if ($1 > $2){
print “The first column is larger”
}
else {
print “The second column is larger”
})
</PRE>
<!-- END CODE SNIP //-->
<P>Here’s the quick-and-dirty method:
</P>
<!-- CODE SNIP //-->
<PRE>
# if syntax from hell
$1 > $2{
print “The first column is larger”
}
{print “The second column is larger”)
</PRE>
<!-- END CODE SNIP //-->
<P>You may notice that the keywords <TT>if</TT> and <TT>else</TT> are left off. The general structure is retained: expression, true commands, and false commands. However, this is much less readable if you don’t know that it is an <TT>if</TT> statement! Not all versions of <TT>gawk</TT> allow this method of using <TT>if</TT>, so don’t be too surprised if it doesn’t work. Besides, you should be using the more verbose method of writing <TT>if</TT> statements for readability’s sake.</P>
<H4 ALIGN="LEFT"><A NAME="Heading17"></A><FONT COLOR="#000077">The while Loop</FONT></H4>
<P>The <TT>while</TT> statement allows a set of commands to be repeated as long as some condition is true. The condition is evaluated each time the program loops. The general format of the <TT>gawk while</TT> loop is as follows:</P>
<!-- CODE SNIP //-->
<PRE>
while (<I>expression</I>){
<I>commands</I>
}
</PRE>
<!-- END CODE SNIP //-->
<P>For example, the <TT>while</TT> loop can be used in a program that calculates the value of an investment over several years (the formula for the calculation is value=amount(1+interest_rate)^years):</P>
<!-- CODE SNIP //-->
<PRE>
# interest calculation computes compound interest
# inputs from a file are the amount, interest_rate, and years
{var = 1
while (var <= $3) {
printf(“%f\n”, $1*(1+$2)^var)
var++
}
}
</PRE>
<!-- END CODE SNIP //-->
<P>You can see in this script that we initialize the variable <TT>var</TT> to 1 before entering the <TT>while</TT> loop. If we don’t do this, <TT>gawk</TT> assigns a value of zero. The values for the three variables we use are read from the input file. The <TT>autoincrement</TT> command is used to add one to <TT>var</TT> each time the line is executed.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="453-456.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="459-461.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?