I am trying to figure out how to write a standalone awk script file.
I thought it would be similar to a standalone bash script file:
#! /usr/bin/awk -f
BEGIN{
for (i = 0; i < ARGC; i++)
printf "%s ", ARGV[i]
printf "\n"
}
{print $0}
I was trying to figure out how the command line arguments are specified in shell, and passed into the script:
$ myscript.awk arg1 arg2 arg3 awk arg1 arg2 arg3 awk: /home/tim/myscript.awk:5: fatal: cannot open file `arg1' for reading (No such file or directory)
What does an awk script expect its command line arguments to be? Why does it expect
arg1
to be the input file?Command line arguments are passed into an awk script, and stored in array ARGV. See my udpate. So I suppose the command line arguments are interpreted up to the script, not to
awk
.If I remove
-f
in the shebang, i.e.#! /usr/bin/awk
$ myscript.awk arg1 arg2 arg3 awk: cmd. line:1: /home/tim/myscript.awk awk: cmd. line:1: ^ syntax error
Why is
-f
necessary?
Thanks.
arg1
to be (or want it to be)?awk
always takes the target filenames as arguments.ARGV
. See my udpate. So I suppose the command line arguments are interpreted up to the script, not toawk
.{print $0}
is expected to be operating on...?