No need to check ARGC. In general, if I were calling awk from the command line
I'd do:
awk 'BEGIN{ARGV[ARGC]=ARGV[ARGC-1]; ARGC++} NR==FNR{..; next} {..}' file
and from inside a shell script where I have the file name in a variable:
awk 'NR==FNR{..; next} {..}' "$file" "$file"
and only if I for some reason needed a hard-coded file name in an awk script
would I use this (what I posted earlier was just a simplified version of this to
keep it as clear as possible):
awk 'BEGIN{ARGV[ARGC+1]=ARGV[ARGC]="file"; ARGC+=2} NR==FNR{..; next} {..}' file
I see/write code like that frequently so it seemed perfectly clear to me but
thinking about it now I can understand that it's non-obvious the first time you
come across it and worth a comment!
>
>> Awk has an implicit while read loop so it's more awkish to let it just
>> do what it does by default rather than introducing 2 other
>> hand-written while read loops and bypassing the normal awk behavior
>> for that which in turn stops you from using awks basic `condition {
>> action }` syntax and so overall makes your program lengthier, more
>> fragile, and generally more C-like than awk-like.
>
> AWK's inherent behaviour is not really what's needed in this case, but
> if avoiding it is not AWKish, so be it (I am no authority on what's
> idiomatic AWK).
It's extremely common to have to parse the same file twice, often to identify
lines/blocks on the first pass then do something with them on the second pass or
to count something(s) in the first pass and use those counts in the second pass
as we did in this example, and while you can write your own code to bypass it if
you choose, awks inherent behavior is perfectly suited for doing so.
Ed.
>
> <snip>
>