Recently I've encountered a strange behavioral problem with awk
say I have two files one with blank file & the another is with populated data
so let me apply a simple unmatched code
awk -v var=0 'NR==FNR{a[$var]++;next} !($var in a)' file1 file2
say
file1
&
file 2
a
b
v
it will return blank data where as it is supposed to return all the content in file 2.
can someone explain me how to overcome this issue?
There isn't any data in file1
, so the overall record number never changes, so FNR == NR
throughout file2
. I'm not sure there's an easy way to fix that, either.
You can't even use a BEGIN
block to record the current file name and spot when the file name changes. The POSIX specification for awk
says:
FILENAME
A pathname of the current input file. Inside a BEGIN action the value is undefined. Inside an END action the value shall be the name of the last input file processed.
I think your best bet is likely to be comparing FILENAME
with ARGV[1]
:
awk -v var=0 'FILENAME==ARGV[1] {a[$var]++;next} !($var in a)' file1 file2