rhl26.htm
来自「linux的初学电子书」· HTM 代码 · 共 2,804 行 · 第 1/5 页
HTM
2,804 行
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
decrements count by one</FONT>
</TABLE><BR>
<A NAME="E69E326"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Built-In Variables</B></FONT></CENTER></H4>
<BR>
<P>The gawk 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 26.7 shows the important built-in variables.
<BR>
<BR>
<P ALIGN=CENTER>
<CENTER>
<FONT COLOR="#000080"><B>Table 26.7. The important built-in variables.</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>Variable</I>
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Description</I>
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
NR
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
The number of records read so far
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
FNR
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
The number of records read from the current file
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
FILENAME
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
The name of the input file
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
FS
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Field separator (default is whitespace)
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
RS
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Record separator (default is newline)
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
OFMT
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Output format for numbers (default is %g)
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
OFS
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Output field separator
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
ORS
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Output record separator
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
NF
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
The number of fields in the current record</FONT>
</TABLE><P>The NR and FNR values are the same if you are processing only one file, but if you are doing more than one file, NR is a running total of all files, while FNR is the total for the current file only.
<BR>
<P>The FS variable is useful, because it controls the input file's field separator. To use the colon for the /etc/passwd file, for example, you would use the command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">FS=":"</FONT></PRE>
<P>in the script, usually as part of the BEGIN pattern.
<BR>
<P>You can use these built-in variables as you would any other. For example, the command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">NF <= 5 {print "Not enough fields in the record"}</FONT></PRE>
<P>gives you a way to check the number of fields in the file you are processing and generate an error message if the values are incorrect.
<BR>
<BR>
<A NAME="E68E219"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Control Structures</B></FONT></CENTER></H3>
<BR>
<P>Enough of the details have been covered to allow us to start doing some real gawk programming. Although we have not covered all of gawk's pattern and action considerations, we have seen all the important material. Now we can look at writing control
structures.
<BR>
<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 gawk is cleanly laid out without weird
syntax. Follow the examples and try a few test programs of your own.
<BR>
<P>Incidentally, gawk enables you to place comments anywhere in your scripts, as long as the comment starts with a # sign. You should use comments to indicate what is going on in your scripts if it is not immediately obvious.
<BR>
<BR>
<A NAME="E69E327"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>if</B><B> Statement</B></FONT></CENTER></H4>
<BR>
<P>The if statement is used to allow gawk to test some condition and, if it is true, execute a set of commands. The general syntax for the if statement is
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">if (expression) {commands} else {commands}</FONT></PRE>
<P>The expression is always evaluated to see if it is true or false. No other value is calculated for the if expression. Here's a simple if script:
<BR>
<PRE>
<FONT COLOR="#000080"># a simple if loop
(if ($1 == 0){
print "This cell has a value of zero"
}
else {
printf "The value is %d\n", $1
})</FONT></PRE>
<P>You will notice that I used the curly braces to lay out the program in a readable manner. Of course, this could all have been typed on one line and gawk would have understood it, but writing in a nicely formatted manner makes it easier to understand
what is going on, and debugging the program becomes much easier if the need arises.
<BR>
<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 printf statement prints the value of the column.
<BR>
<P>The flow of the if 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 else section. It can be left out entirely, if desired. For example, this
is a complete and valid gawk script:
<BR>
<PRE>
<FONT COLOR="#000080"> (if ($1 == 0){
print "This cell has a value of zero"
})</FONT></PRE>
<P>The gawk language, to be compatible with other programming languages, allows a special format of the if statement when a simple comparison is being conducted. This quick-and-dirty if 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 if statement written the proper way:
<BR>
<PRE>
<FONT COLOR="#000080"># a nicely formatted if loop
(if ($1 > $2){
print "The first column is larger"
}
else {
print "The second column is larger"
})</FONT></PRE>
<P>Here's the quick-and-dirty method:
<BR>
<PRE>
<FONT COLOR="#000080"># if syntax from hell
$1 > $2{
print "The first column is larger"
}
{print "The second column is larger")</FONT></PRE>
<P>You will notice that the keywords if and else are left off. The general structure is retained: expression, true commands, and false commands. However, this is much less readable if you do not know that it is a if statement! Not all versions of gawk will
allow this method of using if, so don't be too surprised if it doesn't work. Besides, you should be using the more verbose method of writing if statements for readability's sake.
<BR>
<BR>
<A NAME="E69E328"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>while</B><B> Loop</B></FONT></CENTER></H4>
<BR>
<P>The while 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 gawk while loop is
<BR>
<PRE>
<FONT COLOR="#000080">while (expression){
commands
}</FONT></PRE>
<P>For example, the while 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):
<BR>
<PRE>
<FONT COLOR="#000080"># 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++
}
}</FONT></PRE>
<P>You can see in this script that we initialize the variable var to 1 before entering the while loop. If we hadn't done this, gawk would have assigned a value of zero. The values for the three variables we use are read from the input file. The
autoincrement command is used to add one to var each time the line is executed.
<BR>
<BR>
<A NAME="E69E329"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>for</B><B> Loop</B></FONT></CENTER></H4>
<BR>
<P>The for loop is commonly used when you want to initialize a value and then ignore it. The syntax of the gawk for loop is
<BR>
<PRE>
<FONT COLOR="#000080">for (initialization; expression; increment) {
command
}</FONT></PRE>
<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's an example of a for loop, which is the same basic program as shown for the while loop:
<BR>
<PRE>
<FONT COLOR="#000080"># interest calculation computes compound interest
# inputs from a file are the amount, interest_rate, and years
{for (var=1; var <= $3; var++) {
printf("%f\n", $1*(1+$2)^var)
}
}</FONT></PRE>
<P>In this case, var is initialized when the for loop starts. The expression is evaluated, and if true, the loop runs. Then the value of var is incremented and the expression is tested again.
<BR>
<P>The format of the for loop might look strange if you haven't encountered programming languages before, but it is the same as the for loop used in C, for example.
<BR>
<BR>
<A NAME="E69E330"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>next and exit</B></FONT></CENTER></H4>
<BR>
<P>The next instruction tells gawk to process the next record in the file, regardless of what it was doing. For example, in this script:
<BR>
<PRE>
<FONT COLOR="#000080">{ command1
command2
command3
next
command4
}</FONT></PRE>
<P>as soon as the next statement is read, gawk 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, command4 will never be executed because the next statement moves back up to
command1 each time.
<BR>
<P>The next statement is usually used inside an if loop, where you may want execution to return to the start of the script if some condition is met.
<BR>
<P>The exit statement makes gawk behave as though it has reached the end of the file, and it then executes any END patterns (if any exist). This is a useful method of aborting processing if there was an error in the file.
<BR>
<BR>
<A NAME="E69E331"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Arrays</B></FONT></CENTER></H4>
<BR>
<P>The gawk language supports arrays and enables you to access any element in the array easily. No special initialization is necessary with an array, because gawk treats it like any other variable. The general format for declaring arrays is
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">var[num]=value</FONT></PRE>
<P>As an example, consider the following script that reads an input file and generates an output file with the lines reversed in order:
<BR>
<PRE>
<FONT COLOR="#000080"># reverse lines in a file
{line[NR] = $0 } # remember each line
END {var=NR # output lines in reverse order
while (var > 0){
print line[var]
var—
}
}</FONT></PRE>
<P>In this simple program (try and do the same task in any other programming language to see how efficient gawk is!), we used the NR (number of records) built-in variable. After reading each line into the array line[], we simply start at the last record
and print them again, stepping down through the array each time. We don't have to declare the array or do anything special with it, which is one of the powerful features of gawk.
<BR>
<BR>
<A NAME="E68E220"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Summary</B></FONT></CENTER></H3>
<BR>
<P>We've only scratched the surface of gawk's abilities, but you might have noticed that it is a relatively easy language to work with and places no special demands on the programmer. That's one of the reasons gawk 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 gawk.
<BR>
<P>If you are a system administrator or simply a power user, you will find that gawk 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 gawk, check the
man pages or one of the few awk books that are available.
<BR>
<P ALIGN=LEFT>
<A HREF="rhl25.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl25.htm" TARGET="_self"><IMG SRC="purprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="#I0" TARGET="_self"><IMG SRC="purtop.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></A>
<A HREF="index-1.htm"
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?