How to debug the awk script

Hi,
How can I debug an awk script?

I know that set -x can be used to debug a script. But this will not suite for awk scripts.

Can anyone help me?

Thanks in advance,
Chella

awk does not come with a built in debug option.

I have come across but never used awkdb:

 [AWK Debugger](http://utopia.knoware.nl/users/awater/index.html)

Usually I just print out various messages or values of fields
to see what is going on.

If you are using gawk, you could recompile the source with
the debugging option turned on to print out the parse stack
information as gawk executes - but this will probably give
you far more information than you can use.

Hi.

This might help, at least in the early stages of developing an awk script:

#!/usr/bin/env sh

# @(#) a1       Demonstrate one debugging feature in [g]awk.

set -o nounset
echo

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash awk

echo

awk --lint '
b == 1  { print " Found case for value of b is 1." }
' data1

exit 0

Producing:

% ./a1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
GNU Awk 3.1.4

awk: cmd. line:2: (FILENAME=data1 FNR=1) warning: reference to uninitialized variable `b'
awk: cmd. line:2: (FILENAME=data1 FNR=2) warning: reference to uninitialized variable `b'
awk: cmd. line:2: (FILENAME=data1 FNR=3) warning: reference to uninitialized variable `b'

However, most awk scripts are so short that it probably is not worth anyone's time to put in a lot of work doing a full debugger (well, it might be worth a grad student's time perhaps :slight_smile: )

See info awk for the situations about which --lint will complain ... cheers, drl