back toc next

Example 2

Emulation of wc -w

BEGIN{ w=0 }
{ w+= NF }
END{ print w }

END{ print w }; { w+=NF }; BEGIN{ w=0 }
would work too.

String manipulation

bash$ awk '{ sub(/[^ ]* */,""); print $0 }'

A simple calculator

BEGIN{ print "type a number" }
{ print $1 "square =" $1*$1 }

Rotating the input column

{ j=1+j%3; print $j }