array access in END block failure

Hi guys i am new to shell scripting. I wrote this script that simply searches a column value of file1 from file2.

please look at the code below:

awk '
FILENAME==ARGV[1] { file_1_data[FNR]=$0; next }
FILENAME==ARGV[2] { file_2_data[FNR]=substr($3,1,12); next }
END {
# do actual data processing here
flag=0
for (i in file_1_data) {
$flag=0
for (j in file_2_data) {
if (substr(file_1_data[i],10,12) == file_2_data[j]) {
print file_1_data [i]> "anni_report_success"
$flag=1
break
}
}
if ($flag == 0) {
print file_1_data [i]> "anni_report_failure"
}
}
}' file1 file2

The problem that i facing is that as soon as script reaches the END block it simply cant access the array's "file_1_data and file_2_data"

The result that i get from shell is
awk: file_1_data is not an array
record number 14304

Your help is requried, i donno why is this happening.

Remove the $ sign before the variable flag.

Regards

Hi Franklin

I did as you mentioned. But all in vain. I am still getting the same error.

file1

a = 167259, total usage = 248810
a = 819080, total usage = 378869
a = 423639, total usage = 166699
a = 118830, total usage = 194917
a = 741620, total usage = 135128
a = 561890, total usage = 177346

file2

a = 167259
a = 819080
a = 323639
a = 118830
a = 741620
a = 961890
a = 074651
a = 244733

------------ The script -----------------------
awk '
FILENAME==ARGV[1] { file_1_data[FNR]=$0; next }
FILENAME==ARGV[2] { file_2_data[FNR]=substr($3,1,12); next }
END {
# do actual data processing here
flag=0
for (i in file_1_data) {
$flag=0
for (j in file_2_data) {
if (substr(file_1_data[i],10,12) == file_2_data[j]) {
print file_1_data [i]> "anni_report_success"
$flag=1
break
}
}
if ($flag == 0) {
print file_1_data [i]> "anni_report_failure"
}
}
}' file1 file2

---------------------------------------------------------

error code that i get is:

awk: file_1_data is not an array
record number xxx

(its the same even after removing the $ sign with variable flag.

Not tested, but you can try this:

awk '
FILENAME==ARGV[1] { file_1_data[FNR]=$0; next }
FILENAME==ARGV[2] { file_2_data[FNR]=substr($3,1,12); next }
END {
# do actual data processing here
  for (i in file_1_data) {
    flag=0
    for (j in file_2_data) {
      if (substr(i,10,12) == j) {
        print i > "anni_report_success"
        flag=1
        break
      }
    }
    if ($flag == 0) {
    print i > "anni_report_failure"
    }
  }
}' file1 file2

Have a read of your awk documentation regarding the use of arrays.
Next time place your code within code tags to improve readability.

Regards

Are you sure the following will be equal?

substr(file_1_data,10,12) == file_2_data[j]

What will substr(file_1_data[i],10,12) be?

OK, I am trying to figure out what you are trying to do.
Here is a possible awk program.
ex.awk

NR==FNR{file_2_data[$3]; next}
{if(substr($0, 5, 6) in file_2_data)
        print $0 > "anni_report_success"
 else
        print $0 > "anni_report_failure"
}
awk -f ex.awk file2 file1

Not sure, if it is what you want, please let me know.