Next: Sample Programs, Previous: Functions, Up: Top [Contents][Index]
awk
awk
FunctionsUser-defined describes how to write
your own awk
functions. Writing functions is important, because
it allows you to encapsulate algorithms and program tasks in a single
place. It simplifies programming, making program development more
manageable and making programs more readable.
In their seminal 1976 book, Software Tools,62 Brian Kernighan and P.J. Plauger wrote:
Good Programming is not learned from generalities, but by seeing how significant programs can be made clean, easy to read, easy to maintain and modify, human-engineered, efficient and reliable, by the application of common sense and good programming practices. Careful study and imitation of good programs leads to better writing.
In fact, they felt this idea was so important that they placed this statement on the cover of their book. Because we believe strongly that their statement is correct, this chapter and Sample Programs, provide a good-sized body of code for you to read and, we hope, to learn from.
This chapter presents a library of useful awk
functions.
Many of the sample programs presented later in this Web page
use these functions.
The functions are presented here in a progression from simple to complex.
Extract Program
presents a program that you can use to extract the source code for
these example library functions and programs from the Texinfo source
for this Web page.
(This has already been done as part of the gawk
distribution.)
If you have written one or more useful, general-purpose awk
functions
and would like to contribute them to the awk
user community, see
How To Contribute, for more information.
The programs in this chapter and in
Sample Programs,
freely use gawk
-specific features.
Rewriting these programs for different implementations of awk
is pretty straightforward:
gawk
.
nextfile
(see Nextfile Statement)
to skip any remaining input in the input file.
IGNORECASE
.
You can achieve almost the same effect63 by adding the following rule to the
beginning of the program:
# ignore case { $0 = tolower($0) }
Also, verify that all regexp and string constants used in comparisons use only lowercase letters.
• Library Names: | How to best name private global variables in library functions. | |
• General Functions: | Functions that are of general use. | |
• Data File Management: | Functions for managing command-line data files. | |
• Getopt Function: | A function for processing command-line arguments. | |
• Passwd Functions: | Functions for getting user information. | |
• Group Functions: | Functions for getting group information. | |
• Walking Arrays: | A function to walk arrays of arrays. | |
• Library Functions Summary: | Summary of library functions. | |
• Library Exercises: | Exercises. |
Sadly, over 35 years later, many of the lessons taught by this book have yet to be learned by a vast number of practicing programmers.
The effects are
not identical. Output of the transformed
record will be in all lowercase, while IGNORECASE
preserves the original
contents of the input record.
Next: Sample Programs, Previous: Functions, Up: Top [Contents][Index]