For example, suppose I run the following command:
gawk -f AppendMapping.awk Reference.tsv TrueInput.tsv
Assume the names of files WILL change. While iterating through the first file, I want to create a mapping.
map[$16]=$18
While iterating through the second file, I want to use the mapping.
print $1, map[$2]
What's the best way to achieve this behavior (ie, different behavior for each input file)?
This might work for you:
So by combining
FILENAME
withARGV[n]
wheren
is the nth file on the command line,awk
can conditionally change individual files.N.B. ARGV[0] would be the
awk
command.As you probably know
NR
stores the current line number; as you may or may not know, it's cumulative - it doesn't get reset between files.FNR
, on the other hand, is specific to the file, so you can use those two to see whether you're in the first file (beyond the second you'll need to keep your own counter).You could also use
getline
in theBEGIN
block to loop through it manually.Gawk versions 4 and high offer the special
BEGINFILE
(andENDFILE
) block as well as the usualBEGIN
andEND
blocks. Use them to set flags on which you vary the behavior of your code.Recall that patterns can include comparisons with variables, so you can select patters directly on the value of your flags.
The man page says: