[Solved] How to debug awk script?

how can i view what variables are stored upon the excution of an awk script.

something equivalent to shell eg. sh -vx "script_file"

many thanks in advance!

You can add print or printf statements in appropriate places in your code.

If you want to build in debugging prints that don't normally print, but can be turned on when needed, you can make the prints only occur when a variable is set to a certain range of values. For example:

awk '
debug {printf("NR=%d, FNR=%d, NF=%d, $0=\"%s\"\n", NR, FNR, NF, $0)}
whatever your script normally does
' file

will do whatever your script normally does, while changing the 1st line to:

awk -v debug=1 '

will print your debugging information for every line read in addition to doing whatever your script normally does.

1 Like