The -i option (including other files in your program)
This option is equivalent to the @include directive, which is used to source a file in the current AWK program. However, it is different from the -f option in two aspects. First, when we use the -i option, the program sourced is not loaded if it has been previously loaded, whereas -f always loads a file. The second difference is this after processing an -i argument, GAWK still expects to find the main source code via the -f option or on the command line:
-i source-file
--include source-file
In the next example, we will use the f1.awk and f2.awk files we created earlier to describe how the -i option works:
$ awk -i f1.awk 'NR==5 { print NR, $0 }' cars.dat
The output on execution of the given code is:
2 honda city 2005 60000 3
5 honda city 2010 33000 6
Now, we are using the -i option to include the f1.awk file inside the -f option to execute f2.awk, as follows:
$ awk -i f1.awk -f f2.awk cars.dat
The output on execution of the preceding code is:
2 honda city 2005 60000 3
4 chevy beat 2005 33000 2
The next example shows it is mandatory to specify the AWK command or main source file using the -f option for executing a program with the -i option:
$ awk -i f1.awk cars.dat
The output on execution of the code is:
awk: cmd. line:1: cars.dat
awk: cmd. line:1: ^ syntax error