AWK: a powerful tool for programmer

2012-10-25
#awk

AWK is an acronym of the first letters of its authors’ names (Aho, Weinberger, and Kernighan). It is a data-manipulating scripting language with huge possibilities. There are several implementations of it: awk is the canonical one, nawk (new awk), mawk (the default in Ubuntu 12.04), and gawk, which is GNU awk. I recommend the latter one, because it works correctly with Unicode symbols, as in the example:

1$ echo юникод | gawk "{res = toupper(\$1); print res;}"
2ЮНИКОД

§ Basic usage

The most useful feature is writing script files to be loaded in awk later. One can execute a script file with

1gawk [options] -f script_file.awk input_file

If there is no input file, awk will read the standard input stream.

Let’s take a look at an example. It reads the input stream, writes down the first argument to history, increases the counter by 1, and prints the result. Code of script.awk:

 1#!/usr/bin/gawk -f
 2# BEGIN block executes only once after running awk
 3BEGIN {
 4    print "\nBegin printing args\n";
 5    i = 0;
 6}
 7
 8# Main block executes for every argument
 9{
10    i++;
11    history[i] = $1;
12    print i, $1;
13    if ($1 == 0)
14        exit(0);
15}
16
17# END block executes only once at finishing awk
18END {
19    print "\nArguments were: ";
20    for (n=1; n<=i; ++n)
21        print history[n]," ";
22    print "\nEnd printing args\n"
23}

Then, make script executable and run it:

1$chmod +x ./script.awk
2$./script.awk

Output will be like (enter “one”, “cat”, “dog”, and “0”):

 1Begin printing args
 2
 3one
 41 one
 5cat
 62 cat
 7dog
 83 dog
 90
104 0
11
12Arguments were:
13one
14cat
15dog
160
17
18End printing args

Awk can be launched with script inline:

1gawk [options] ''script_text'' file(s)

The example counts the “block” words in the code listed above:

1awk "BEGIN{blocks=0} /block/{blocks++} END{ print blocks}" script.awk

Here /regular expression/ controls whether the block after it will be executed.

§ User-defined functions

In awk user-defined functions can be added as follows:

 1#!/usr/bin/gawk -f
 2
 3# returns sum of numbers
 4function sum(a, b, c) {
 5    res = a + b + c;
 6
 7    return res;
 8}
 9
10# main program, for testing
11{
12    print sum($1, $2, $3);
13}