array access in END block failure


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting array access in END block failure
# 1  
Old 06-16-2008
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.

Last edited by fahadaizaz; 06-16-2008 at 01:16 PM.. Reason: missing information
# 2  
Old 06-16-2008
Remove the $ sign before the variable flag.

Regards
# 3  
Old 06-17-2008
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.
# 4  
Old 06-17-2008
Not tested, but you can try this:

Code:
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
# 5  
Old 06-17-2008
Are you sure the following will be equal?

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

What will substr(file_1_data[i],10,12) be?
# 6  
Old 06-17-2008
OK, I am trying to figure out what you are trying to do.
Here is a possible awk program.
ex.awk
Code:
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"
}

Code:
awk -f ex.awk file2 file1

Not sure, if it is what you want, please let me know.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Add a block of code at the end of a specific block

I need to search for a block with the starting pattern say "tabId": "table_1", and ending pattern say "]" and then add a few lines before "]" "block1":"block_111" "tabId": "table_1", "title":"My title" ..... .... }] how do I achieve it using awk and sed. Thanks, Lakshmi (3 Replies)
Discussion started by: Lakshmikumari
3 Replies

2. Shell Programming and Scripting

Unable To access array in awk

Hi, i have the following code in which i am passing array tldn in awk using -v option & despite of that condition is not getting matched,can somebody suggest how to handle shell arrays in awk tcount=(9875 9667) awk -F"\t" -v ltldn="${tldn}" 'NR==FNR {POSTPAIDMDNS=$2"|"$3;next} ... (6 Replies)
Discussion started by: siramitsharma
6 Replies

3. Shell Programming and Scripting

Add value to end of array

I'm trying to expand my array by adding another value onto the end of it, thus adding a new index and upping the length of the array by one. I am iterating through two arrays, and trying to get one array into the index of the other. I'm use 4.1.5 release of bash and most of the methods I have tried... (4 Replies)
Discussion started by: jrymer
4 Replies

4. Programming

Access a value in 2D array in C program

Hi All, I am new to c programming. I am getting compilation error in the below program. Can somebody help me? #include<stdio.h> #include<string.h> void main() { int i=j=0; char a={'f1',4,'f2','4'}; char count; for(i=0;i<2;i++) { for(j=1;j<=2;j++) { ... (2 Replies)
Discussion started by: sam_14189
2 Replies

5. Shell Programming and Scripting

problem in access in array variables

hi all, its me again!!! i've requirement like this: i want to create a file & an array with its name having the filename as its substring. here is the test script!! #!/bin/bash touch $1 declare -a $1_rec; echo -n "$1_rec: " read $1_rec; echo $]; now see output: this is when i enter... (7 Replies)
Discussion started by: tprayush
7 Replies

6. Solaris

Unable to access 3500 FC array. Where is the problem?..

Hi, I have two Sun Fire V490 with Solaris 10 5/08, FC switch and two Sun StorageTek 3500 FC arrays. Each array is connected to switch and to one server at a time. In the last week I installed Solaris 10 5/08 on both servers and set up Sun Cluster, version 3.2. At wednesday all was fine - all... (7 Replies)
Discussion started by: Sapfeer
7 Replies

7. Shell Programming and Scripting

Accessing single elements of a awk array in END

How do I access one of the indices in array tst with the code below? tst=sprintf("%5.2f",Car / 12) When I scan thru the array with for ( i in tst ) { print i,tst } I get the output of: vec-7 144 But when I try this in the END print tst It looks like it's not set. What am... (6 Replies)
Discussion started by: timj123
6 Replies

8. Linux

attempt to access beyond end of device

Hi, we have running 8 box sles 9 cluster and on an nfs filesystem we have the problem which is grepped from /var/log/messages. Jun 8 13:40:46 qnclpx02 kernel: attempt to access beyond end of device Jun 8 13:40:46 qnclpx02 kernel: sdat: rw=0, want=8894615912, limit=314572800 Is there... (1 Reply)
Discussion started by: ortsvorsteher
1 Replies

9. UNIX for Dummies Questions & Answers

Access value outside awk or split value of array

Hello I am new to Unix. Please help me out. My Scenario: I am first collecting all the file names present in the directory with structure myinfo/yourinfo/supplierinfo I have four files with the names myCollector.java, yourCollector.java, someCollector.java, everyCollector.java. in the directory.... (1 Reply)
Discussion started by: jason.bean
1 Replies
Login or Register to Ask a Question