Small awk programs can be tested by running them thru any awk interpreter on a UNIX system. awk has also been ported to other platforms and can also be used to test smallawk programs. Suppose your smallawk program is in a file called
hello.awkthen the command
awk -f hello.awkwill test the program.
Like awk, smallawk is designed for processing data files. It works with simpler files than awk however. Smallawk assumes that it will read a single file and each line in the file is a list of "fields" of data separated by one or more spaces or tabs. Smallawk reads the inoput file and produces a single stream of output by processing the input. Notice that a smallawk program does not start until it is given some data, and doesn't stop until it gets the usual end-of-file. Here is a traditional simple program in smallawk:
END{ print "hello, world"; }
It reads the input and at the end of the inpoyut data it outputs the
"hello, world message. The following program reads the input file
and numbers the lines:
{ print NR, $0; }
The next program prints outs lines that contain the string "AWK":
/AWK/{ print ;}
The next program Assumes that each line has one number, and at the end of the file outputs the total of these nunmbers:
{sum = sum + $0;}
END{print sum;}
This one checks the input and only adds a line if it is a valid integer, with one or more decimal digits:
/^[0-9][0-9]*$/{sum = sum + $0;}
END{print sum;}
This reads in a file of names, student_ids, and scores and calculates the mean score. It assume input with data separated by spaces like this:
ShortName 9999 3.2
AnotherName 1234 17The program is
{sum = sum + $3; count=count+1;}
END{print sum/count;}
/Botting/||/Dick/
/Botting/ && /Richard/
!/Blotting/
/Banana/
/[bB]anana/
/^\.As_is/
/^[0-9]*$/
^.As_is
two part:$
^Dick.*Botting$
pos*ibil*ityThe "*" means "zero or more of". The above matches "posssibiity" for example.
Actions
An action is a series of at least one statement. These are
executed one after another.
sum = sum + $0;
print sum;
if ( sum > 0 ) print "greater"; else ;
$0
$5
sum
. . . . . . . . . ( end of section Syntax.) <<Contents | End>>
Here are the informal operational semantics of a smallawk program P. P wll consist of a sequence of n pieces p[1]..p[n]. Each piece 'p'['i'] has two parts a pattern 'p'['i'].pattern and an action 'p'['i'].action.
Here is a diagram:
Here is a C++like description of what the program P does.
NR=1;
while( get next line until end of input )
{
for(i = 1; i<=n; i++)
if( line matches p[i].pattern )
apply p[i].action to line;
NR++;
}
//after end of file
for(i = 1; i<=n; i++)
if( p[i].pattern is "END" )
apply p[i].action;
A line matches a pattern according to the rules TBD ... [ regular_expressions.html ]
Applying an action to the line starts by assigning the whole line to variable $0. Then each field in the line (separated by one or more spaces) is assigned to $1 thru to NF where NF is set to the number of fields. An action is a sequence of one or more instructions and these are executed in turn. If an instruction is an assignment then the expression on the right hand side is evaluated and the resulting value is placed in the variable on the left hand side of the '=' sign. This may change the whole line or any field in the line if the variable is '$0' or 'i' for some other i. If the action is a print command with an expression then the expression is evaluated and output plus a new line. If it is a print with no expression then the whole line (with any changes) is printed. If the instruction is a selection with condition c and body b and else part 'e', then the condition is evaluated and if it not zero the body b is executed. Other wise if c evaluates to zero then e is executed.
Expressions are evaluated in the usual way: constants beome their values, variables return their current value, and operations are applied in order of precedence to give a value.
Authors
Richard J Botting
rbotting@csusb.edu
. . . . . . . . . ( end of section Small Awk) <<Contents | End>>