this is obsolete doc -- see http://doc.nethence.com/ instead
AWK examples
Usage
To execute some AWK code,
awk '{code here}'
awk -F'delimiter' '{code here}'
awk ' BEGIN {preproc code here}
{code here}
END {postproc code here}'
Propeller
To make a propeller acting on stdin,
BEGIN {split("|#/#-#\\", p, "#"); i = 1}
{
printf("%c\b", p[i++]);
if (i > 4) {i = 1;};
}
END {printf(" done\n");}
IP/shortmask
On Linux systems, print IP and short form netmask,
/sbin/ifconfig -a \
| awk '/inet addr/ {
ip=gensub(/.*inet addr:([0-9.]+).*/,"\\1","");
mask=gensub(/.*Mask:([0-9.]+).*/,"\\1","");
split(mask,V,".");
n=0;
for(i=1; i<=4; i++) {
while(V[i]) {
n+=V[i]%2;
V[i]=int(V[i]/2);
};
};
print ip "/" n}'
Miscellaneous
To delete the last field from a record,
echo "1 2 3" | awk '{$NF=""}{print}'
References
AWK by example : http://www.ibm.com/developerworks/linux/library/l-awk1.html
Reference : http://www.gnu.org/software/gawk/manual/
FAQ : http://hibernia.jakma.org/~paul/awk-faq.html
Tutorial : http://www.grymoire.com/Unix/Awk.html | Wiki: http://awk.freeshell.org/
(FR) A few examples : http://www.epons.org/awk.php
AWK Tail : http://student.northpark.edu/pemente/awk/awktail.txt
Freenode #awk wiki : http://awk.freeshell.org/
Alfred Aho interview : http://www.computerworld.com.au/index.php/id;1726534212;pp;1;fp;;fpid;