Go to the previous, next section.
Useful awk
programs are often short, just a line or two. Here is a
collection of useful, short programs to get you started. Some of these
programs contain constructs that haven't been covered yet. The description
of the program will give you a good idea of what is going on, but please
read the rest of the manual to become an awk
expert!
awk '{ if (NF > max) max = NF }
END { print max }'
awk 'length($0) > 80'
awk 'NF > 0'
awk '{ if (NF > 0) print }'
awk 'BEGIN { for (i = 1; i <= 7; i++)
print int(101 * rand()) }'
ls -l files | awk '{ x += $4 } ; END { print "total bytes: " x }'
expand file | awk '{ if (x < length()) x = length() }
END { print "maximum line length is " x }'
expand
program to change tabs into spaces,
so the widths compared are actually the right-margin columns.
awk 'BEGIN { FS = ":" }
{ print $1 | "sort" }' /etc/passwd
awk '{ nlines++ }
END { print nlines }'
awk 'END { print NR }'
awk
do the work.
awk '{ print NR, $0 }'
Go to the previous, next section.