The -f option (read source file)
This option is used to read the program source from the source file instead of in the first non-option argument. If this option is given multiple times, the program consists of the concatenation of the contents of each specified source file:
-f source-file
--file=source-file
First, we will create 2 programs to print line number 2 and line number 4, respectively. Then, we will use the -f option to source those files for execution with the interpreter, as follows:
$ vi f1.awk
NR==2 { print NR, $0 }
$ vi f2.
NR==4 { print NR, $0 }
Now, first use only f1. for sourcing:
$ awk -f f1.awk cars.dat
This can also be performed as follows:
awk --file=f1.awk cars.dat
The output on execution of this code is as follows:
2 honda city 2005 60000 3
Now, we will source both the files together. AWK will concatenate the contents of the two sourced files and execute them on the cars.dat filename, as follows:
$ awk -f f1.awk -f f2.awk cars.dat
This can also be performed as follows:
$ awk --file=f1.awk --file=f2.awk cars.dat
The output on execution of this code is as follows:
2 honda city 2005 60000 3
4 chevy beat 2005 33000 2