The first point to note about awk variables is that there are 2 types: numbers and strings.
There is no requirement to declare a variable before using it. Simply using the variable will declare and initialise it.
Awk will initialise a variable to be 0 for numbers and to the empty string "" for strings. For example,
{
a++;
b=b"hello";
print a" "b;
}
will declare a variable called 'a', which will be initialised to 0, then incremented. So the result we expect is for the program to print '1'. In this case, awk knew to initialise 'a' to 0 because we are applying an arithmetic operator to it (the ++ operator).
In the example above we are also concatenating the string "hello" onto the end of the variable b. In this case, since b is uninitialised, awk assumes b is an empty string, so the final result is that b will simply contain the string "hello".
For larger programs, it may best to itialise all your variables beforehand simply for readability.
Awk Built-in Variables
There are many variables that are automatically initialised by awk during the execution of a program.
The first variables to be introduced will be the NR and NF variables. These variables can be used just as any normal integer variable
When awk reads a line it breaks the line into fields, the number of which is recorded in the NF variable.
The most commonly used of these are $1, $2 etc. for referring to input fields.
{ print $1" "$3" "$(NR-4); }
When awk reads in a line automatically breaks up the input line into fields which can be accessed using the $i notation. $1 refers to the first field, $2 refers to the second field etc.