AWK programming lesson 6

Sad as it may be to contemplate, sometimes AWK by itself just isnt enough. Commonly, people want to integrate awk with a larger shellscript. Here are some common ways to accomplish that goal.

Straight output

Sometimes, you just want to use awk as a formatter, and dump the output stright to the user. The following script takes a list of users as its argument, and uses awk to dump information about them out of /etc/passwd.

Note: observe where I unquote the awk expression, so that the shell does expansion of $1, rather than awk.

#!/bin/sh

while [ "$1" != "" ] ; do
	awk -F: '$1 == "'$1'" { print $1,$3} ' /etc/passwd
	shift
done

Setting a shell variable from awk output

Sometimes you just want to use awk as a quick way to set a value for a variable. Using the passwd theme, we have a way to grab the shell for a user, and see if it is in the list of official shells.

Again, be aware of how I unquote the awk expression, so that the shell does expansion of $1, rather than awk.

#!/bin/sh

user="$1"
if [ "$user" ="" ] ; then echo ERROR: need a username ; exit ; fi

usershell=`awk -F: '$1 == "'$1'" { print $7} ' /etc/passwd`
grep -l $usershell /etc/shells
if [ $? -ne 0 ] ; then
	echo ERROR: shell $usershell for user $user not in /etc/shells
fi

Other alternatives:

# See "man regex"
usershell=`awk -F: '/^'$1':/ { print $7} ' /etc/passwd`

#Only modern awks take -v. You may have to use "nawk" or "gawk"
usershell=`awk -F: -v user=$1 '$1 == user { print $7} ' /etc/passwd`

The explaination of the extra methods above, is left as an excercise to the reader :-)

In a pipe-line

Sometimes, you just want to put awk in as a filter for data, either in a larger program, or just a quickie one-liner from your shell prompt. Here's a quickie to look at the "Referrer" field of weblogs, and see what sites link to your top page many different types of web browsers come to look at your site.
#!/bin/sh

grep -h ' /index.html' $* | awk -F\" '{print $4}' | sort -u

This is probably the last of the AWK lessons, simply because I think this is all the syntax AWK has. I'd be happy to give some task-specific example, if you email me with some interesting problem for awk.

AWK summary

All the features I have mentioned, make AWK a fairly decent language. Its main drawback is that it is so line-oriented. It would be kind of nice to have a proceedural language with all the power of AWK. Which is why perl was invented.

Unfortunately, Larry then decided to go waaaaay beyond the simple concept, by throwing in the kitchen sink, AND destroying the cleanness of the AWK language syntax, all in the name of reducing the number of keystrokes needed to accomplish something. The extra functions in perl are good. The syntax, however, is disgusting to programmers capable of touch-typing more than 20 words a minute. Having the shortest number of characters to implement something, should not be the judge of value for a language.

Here endeth the lesson


Author: phil@bolthole.com
Top of AWK lessons - Prev: Chapter 5
bolthole main page