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 }'
- The content of the variables NF and $n is reassigned.
- It is no more possible to reference substrsings of REs (like \1 in sed)
- gsub() for global substitution
A simple calculator
BEGIN{ print "type a number" }
{ print $1 "square =" $1*$1 }
Rotating the input column
{ j=1+j%3; print $j }