www.delorie.com/gnu/docs/gawk/gawk_171.html   search  
Buy the book!

The GNU Awk User's Guide

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

13. A Library of awk Functions

User-Defined Functions, 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.

One valuable way to learn a new programming language is to read programs in that language. To that end, this chapter and Practical awk Programs, provide a good-sized body of code for you to read, and hopefully, 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.

Extracting Programs from Texinfo Source Files, 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 author's collection of awk programs, see How to Contribute, for more information.

The programs in this chapter and in Practical awk Programs, freely use features that are gawk-specific. Rewriting these programs for different implementations of awk is pretty straightforward.

Diagnostic error messages are sent to `/dev/stderr'. Use `| "cat 1>&2"' instead of `> "/dev/stderr"' if your system does not have a `/dev/stderr', or if you cannot use gawk.

A number of programs use nextfile (see section Using gawk's nextfile Statement) to skip any remaining input in the input file. Implementing nextfile as a Function, shows you how to write a function that does the same thing.

Finally, some of the programs choose to ignore upper- and lowercase distinctions in their input. They do so by assigning one to IGNORECASE. You can achieve almost the same effect(50) 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.

13.1 Naming Library Function Global Variables  How to best name private global variables in library functions.
13.2 General Programming  Functions that are of general use.
13.3 Data File Management  Functions for managing command-line data files.
13.4 Processing Command-Line Options  A function for processing command-line arguments.
13.5 Reading the User Database  Functions for getting user information.
13.6 Reading the Group Database  Functions for getting group information.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003