May 102012
 

The Linux shell is a very quick, easy and powerful way of processing text. The AWK interpreter in particular is a lightweight text manipulator and can eaisly be called directly from the command line or a shell script. However, one thing that isn’t immediately obvious is how, within an awk block, to use variables that exist outside it, either from command line environment variable or shell variables set within the calling script.

An example will probably show more clearly what I mean. The following script is intended to take a number as an argument, and then print every Username which has an ID greater than this. Remember that Bash uses numeric variables like “$1” to represent positional invocation arguments, whereas AWK uses them to denote matched fields.

#!/bin/bash
# greater_users.sh
# Usage: ./greater_users.sh UID

uid=$1
awk -v VAR="$uid" -F: '$3>VAR {print $1}' /etc/passwd

Note that the use of VAR inside the AWK block has no “$” sign – AWK doesn’t define variables like this.

The thing to note is that during the execution of AWK, the -v switch is used to assign the value of an external variable, $uid (which was derived from the greater_users.sh script argument), to an AWK internal varable, which I’ve called VAR. VAR can then be called inside the AWK block, in this case to compare it to the third field of the /etc/passwd file, delimited by the colon (:).

The AWK -v switch is a nice bit of glue to help stick your little command line hacks together.


Matt Parsons is a freelance Linux specialist who has designed, built and supported Unix and Linux systems in the finance, telecommunications and media industries.

He lives and works in London.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>