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
Latex disadvantages

LATEX also has some disadvantages, and I guess it’s a bit difficult for me to find any sensible ones, though I am sure other people can tell you hundreds
![]()
- LATEX does not work well for people who have sold their souls . . .
- Although some parameters can be adjusted within a predefined document layout, the design of a whole new layout is difficult and takes a lot of time.
- It is very hard to write unstructured and disorganized documents.
- Your hamster might, despite some encouraging first steps, never be able to fully grasp the concept of Logical Markup.
From the “The Not So Short Introduction to LATEX“
Learning to touch type

I finally got so bored of my typos to start learning to touch type.
For now it’s like trying a new way to walk, when you are used to do it in a different way for like 20 years. Pretty painful.
P.S. This post was touch typed.
