📄 0574-0577.html
字号:
<HTML
<HEAD>
<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:gawk Programming</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311739 //-->
<!-- TITLE=RED HAT LINUX 2ND EDITION //-->
<!-- AUTHOR=DAVID PITTS ET AL //-->
<!-- PUBLISHER=MACMILLAN //-->
<!-- IMPRINT=SAMS PUBLISHING //-->
<!-- PUBLICATION DATE=1998 //-->
<!-- CHAPTER=27 //-->
<!-- PAGES=0545-0582 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0571-0573.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0578-0580.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-574"><P>Page 574</P></A>
<P>To append data to an existing file, you use the following:
</P>
<!-- CODE SNIP //-->
<PRE>printf ("hello world\n") >> "datafile"
</PRE>
<!-- END CODE SNIP //-->
<H5><A NAME="ch27_ 39">
Output to a Command
</A></H5>
<P>In addition to redirecting your output to a file, you can send the output from your program
to act as input for another command. You can code something like the following:
</P>
<!-- CODE SNIP //-->
<PRE>printf ("hello world\n") | "sort -t`,`"
</PRE>
<!-- END CODE SNIP //-->
<P>Any other output statements that pipe data into the same command will specify exactly
the same command after the pipe character (|) because that is how
awk keeps track of which command is receiving which output from your
program.
</P>
<H5><A NAME="ch27_ 40">
Closing an Output File or Pipe
</A></H5>
<P>Whenever you send output to a file or pipe, you should close it when you are done
processing the data. There is a maximum number of open files allowed to
awk that varies with operating system version or individual account configuration (a pipe counts as a file). By closing
files when you are done with them, you reduce the chances of hitting the limit.
</P>
<P>The syntax to close a file is simply
</P>
<!-- CODE SNIP //-->
<PRE>close ("filename")
</PRE>
<!-- END CODE SNIP //-->
<P>where filename is the one specified on the output statement (which can also be
stdout, a variable that contains the filename, or the exact command used with
a pipe).
</P>
<H3><A NAME="ch27_ 41">
Functions
</A></H3>
<P>In addition to the built-in functions (like
gsub or srand), gawk allows you to write your own. User-defined functions are a means of creating a block of code that is accessed in multiple
places in your code. They can also be used to build a library of commonly used routines so you do
not have to recode the same algorithms repeatedly.
</P>
<P>User-defined functions are not a part of the original
awk—they were added to nawk and are supported by
gawk.
</P>
<P>There are two parts to using a function: the definition and the call. The function
definition contains the code to be executed (the function itself) and the call temporarily transfers
from the main code to the function. There are two ways that command execution is transferred
back to the main code: implicit and explicit returns. When
gawk reaches the end of a function (the close curly brace
[}]), it automatically (implicitly) returns control to the calling routine. If
you want to leave your function before the bottom, you can explicitly use the return statement
to exit early.
</P>
<A NAME="PAGENUM-575"><P>Page 575</P></A>
<H4><A NAME="ch27_ 42">
Function Definition
</A></H4>
<P>The general form of a gawk function definition looks like the following:
</P>
<!-- CODE SNIP //-->
<PRE>function functionname(parameter list) {
the function body
}
</PRE>
<!-- END CODE SNIP //-->
<P>You code your function just as if it were any other set of action statements and can place
it anywhere you would put a pattern/action set. If you think about it, the
function functionname(parameter list) portion of the definition could be considered a pattern
and the function body the action.
</P>
<P>
<TABLE BGCOLOR="#FFFF99">
<TR><TD><B>
NOTE
</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
gawk supports another form of function definition where the
function keyword is abbreviated to func. The remaining syntax is the same:
func functionname(parameter list) {
the function body
}
</BLOCKQUOTE></TD></TR>
</TABLE></CENTER>
</P>
<P>Listing 27.5 shows the defining and calling of a function.
</P>
<P>Listing 27.5. Defining and calling functions.
</P>
<!-- CODE //-->
<PRE>BEGIN { print_header() }
function print_header( ) {
printf("This is the header\n");
printf("this is a second line of the header\n");
}
This is the header
this is a second line of the header
</PRE>
<!-- END CODE //-->
<P>The code inside the function is executed only once—when the function is called from
within the BEGIN action. This function uses the implicit return method.
</P>
<P>
<TABLE BGCOLOR="#FFFF99">
<TR><TD><B>
CAUTION
</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
When working with user-defined functions, you must place the parentheses that contain
the parameter list immediately after the function name when calling that function. When
you use the built-in functions, this is not a requirement.
</BLOCKQUOTE></TD></TR>
</TABLE></CENTER>
</P>
<A NAME="PAGENUM-576"><P>Page 576</P></A>
<H4><A NAME="ch27_ 43">
Function Parameters
</A></H4>
<P>Like C, gawk passes parameters to functions by value. In other words, a copy of the
original value is made and that copy is passed to the called function. The original is untouched, even
if the function changes the value.
</P>
<P>Any parameters are listed in the function definition separated by commas. If you have no
parameters, you can leave the parameter list (contained in the parentheses) empty.
</P>
<P>Listing 27.6 is an expanded version of Listing 27.5; it shows the pass-by-value nature of
gawk function parameters.
</P>
<P>Listing 27.6. Passing parameters.
</P>
<!-- CODE //-->
<PRE>BEGIN { pageno = 0;
print_header(pageno);
printf("the page number is now %d\n", pageno);
}
function print_header(page ) {
page++;
printf("This is the header for page %d\n", page);
printf("this is a second line of the header\n");
}
This is the header for page 1
this is a second line of the header
the page number is now 0
</PRE>
<!-- END CODE //-->
<P>The page number is initialized before the first call to the
print_header function and incremented in the function. But when it is printed after the function call, it remains at the original value.
</P>
<P>
<TABLE BGCOLOR="#FFFF99">
<TR><TD><B>
CAUTION
</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
gawk does not perform parameter validation. When you call a function, you can list more
or fewer parameters than the function expects. Any extra parameters are ignored, and
any missing ones default to zero or empty strings (depending on how they are used).
</BLOCKQUOTE></TD></TR>
</TABLE></CENTER>
</P>
<P>
<TABLE BGCOLOR="#FFFF99">
<TR><TD><B>
TIP
</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
You can take advantage of the lack of function parameter validation. It can be used
to create local variables within the called function—just list more variables in the
function definition than you use in the function call. I strongly suggest that you comment the fact
that the extra parameters are really being used as local variables.
</BLOCKQUOTE></TD></TR>
</TABLE></CENTER>
</P>
<A NAME="PAGENUM-577"><P>Page 577</P></A>
<P>There are several ways that a called function can change variables in the calling
routines—through explicit return or by using the variables in the calling routine directly. (These variables
are normally global anyway.)
</P>
<H4><A NAME="ch27_ 44">
The return Statement (Explicit Return)
</A></H4>
<P>If you want to return a value or leave a function early, you need to code a
return statement. If you don't code one, the function will end with the close curly brace
(}). Personally, I prefer to code them at the bottom.
</P>
<P>If the calling code expects a returned value from your function, you must code the
return statement in the following form:
</P>
<!-- CODE SNIP //-->
<PRE>return variable
</PRE>
<!-- END CODE SNIP //-->
<P>Expanding on Listing 27.6 to let the function change the page number, Listing 27.7 shows
the use of the return statement.
</P>
<P>Listing 27.7. Returning values.
</P>
<!-- CODE //-->
<PRE>BEGIN { pageno = 0;
pageno = print_header(pageno);
printf("the page number is now %d\n", pageno);
}
function print_header(page ) {
page++;
printf("This is the header for page %d\n", page);
printf("this is a second line of the header\n");
return page;
}
This is the header for page 1
this is a second line of the header
the page number is now 1
</PRE>
<!-- END CODE //-->
<P>The updated page number is returned to the code that called the function.
</P>
<TABLE BGCOLOR=#FFFF99><TR><TD><B>NOTE</B></TD></TR><TR><TD>
<BLOCKQUOTE>
The return statement allows you to return only one value back to the calling
routine.
</BLOCKQUOTE>
</TD></TR></TABLE>
<H3><A NAME="ch27_ 45">
Writing Reports
</A></H3>
<P>Generating a report in awk entails a sequence of steps, with each step producing the input
for the next step. Report writing is usually a three-step process: Pick the data, sort the data,
and make the output pretty.
</P>
<P><CENTER>
<a href="0571-0573.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0578-0580.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -