14.3.3 Viewing and Changing Data

The commands for viewing and changing variables inside of gawk are:

display [var | $n]

Add variable var (or field $n) to the display list. The value of the variable or field is displayed each time the program stops. Each variable added to the list is identified by a unique number:

gawk> display x
-| 10: x = 1

This displays the assigned item number, the variable name, and its current value. If the display variable refers to a function parameter, it is silently deleted from the list as soon as the execution reaches a context where no such variable of the given name exists. Without argument, display displays the current values of items on the list.

eval "awk statements"

Evaluate awk statements in the context of the running program. You can do anything that an awk program would do: assign values to variables, call functions, and so on.

NOTE: You cannot use eval to execute a statement containing any of the following: exit, getline, next, nextfile, or return.

eval param, …
awk statements

This form of eval is similar, but it allows you to define “local variables” that exist in the context of the awk statements, instead of using variables or function parameters defined by the program.

print var1[, var2 …]
p var1[, var2 …]

Print the value of a gawk variable or field. Fields must be referenced by constants:

gawk> print $3

This prints the third field in the input record (if the specified field does not exist, it prints ‘Null field’). A variable can be an array element, with the subscripts being constant string values. To print the contents of an array, prefix the name of the array with the ‘@’ symbol:

gawk> print @a

This prints the indices and the corresponding values for all elements in the array a.

printf format [, arg …]

Print formatted text. The format may include escape sequences, such as ‘\n’ (see Escape Sequences). No newline is printed unless one is specified.

set var=value

Assign a constant (number or string) value to an awk variable or field. String values must be enclosed between double quotes ("").

You can also set special awk variables, such as FS, NF, NR, and so on.

watch var | $n ["expression"]
w var | $n ["expression"]

Add variable var (or field $n) to the watch list. The debugger then stops whenever the value of the variable or field changes. Each watched item is assigned a number that can be used to delete it from the watch list using the unwatch command.

With a watchpoint, you may also supply a condition. This is an awk expression (enclosed in double quotes) that the debugger evaluates whenever the watchpoint is reached. If the condition is true, then the debugger stops execution and prompts for a command. Otherwise, gawk continues executing the program.

undisplay [n]

Remove item number n (or all items, if no argument) from the automatic display list.

unwatch [n]

Remove item number n (or all items, if no argument) from the watch list.