Awk by examples

AWK is a language for processing files of text. A file is treated as a sequence of records, and by default each line is a record. Each line is broken up into a sequence of fields, so we can think of the first word in a line as the first field, the second word as the second field, and so on. An AWK program is of a sequence of pattern-action statements. AWK reads the input a line at a time. A line is scanned for each pattern in the program, and for each pattern that matches, the associated action is executed.
Basics
-
Print /etc/mtab (awk reads a line at time and prints it) :
awk '{ print }' /etc/mtab
-
Print /etc/mtab ($0 denotes the whole line) :
awk '{ print $0 }' /etc/mtab
-
List mounted filesystems ($1 denotes the first element of the line) :
awk '{ print $1 }' /etc/mtab
-
List groups (-F chooses the Field Separator) :
awk -F":" '{ print $1 }' /etc/group
-
List groups and id (awk concatenates print() arguments) :
awk -F":" '{ print $1 " " $3 }' /etc/group
-
List groups and id (Nicer format) :
awk -F":" '{ print "group: " $1 "\tid: " $3 }' /etc/group
-
Launching external awk scripts :
# first.awk BEGIN { FS=":" } { print $1 }
awk -f first.awk /etc/mtab
-
List IPv4 addressess :
ifconfig | awk '/inet / { print $2 }'
-
List processes run by root :
# psroot.awk $1 == "root" { printf("ROOT: "); for (i=11; i<=NF; i++) printf("%s ", $i); printf("\n") }
ps au | awk -f psroot.awk
-
Print X warnings :
awk '$1 ~ /(WW)/ { print }' /var/log/Xorg.0.log
-
Print a file removing comments :
awk '! /^#/ { print }' /etc/fstab
-
Print number of files/directories :
ls -lA | awk 'BEGIN{ x=0 } { x=x+1 } END{ print x-1 }'
-
Awk as calculator :
echo | awk '{ print ((2*5^2-1)%7) }'
-
Count empty lines :
# blanklines.awk BEGIN { x=0 } /^$/ { x=x+1 } END { print "I found " x " blank lines :)" }
awk -f blanklines.awk /etc/profile
-
Use regexp as FS (note the difference though) :
echo ' a b c d '| awk -F"[ \t\n]+" '{ print $2 }'
echo ' a b c d '| awk -F" " '{ print $2 }'
-
Understand NR and NF :
# nrnf.awk BEGIN { x=0 } { print "Words on line " NR ": " NF ; x+=NF } END { print "Total lines: " NR ; print "Total words: " x }
awk -f nrnf.awk nrnf.awk
leetness
ma è un post divulgativo o che?
@steffoz: Note to self.