Nethence Documentation Lab Webmail Your IP BBDock  


Those documents are obsolete, please use the Nethence Documentation instead.

HomeUnixWindowsOracleObsoleteHardwareDIYMechanicsScriptsConfigsPrivate

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", p[i++]);
if (i > 4) {i = 1;};
}
END {printf(" done ");}
 
 
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
Reference
FAQ
Tutorial : http://www.grymoire.com/Unix/Awk.html | Wiki
(FR) A few examples
AWK Tail
Freenode #awk wiki
Alfred Aho interview
 

Last update: Sep 01, 2009