9

I am trying to use this awk command:

awk -F: '
FILENAME==ARGV[1] { 
    a[FNR]=$1
}
FILENAME==ARGV[2] { 
    for(i=1;i<=length(a);i++) { 
        if(match($0,a[i])) { 
            print a[i],$1
        }
    }
}' 16.passwd 16.group | sort

But got:

awk: line 1: illegal reference to array a
1
  • I also had this problem for if (!a) …, which is essentially the same issue. Commented Sep 11, 2018 at 16:39

3 Answers 3

14

Your problem is this:

length(a)

Using length(array) to get the number of elements in an array is a GNU awk extension and apparently you aren't using GNU awk. Change:

for(i=1;i<=length(a);i++)

to

for(i=1;i in a;i++)

and it should work.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Ed. It works after I change to for(i=1;i in a;i++). BTW, How do I know if my system (Ubuntu 12.04) is using GNU awk?
Indeed, POSIX defines length only for strings, not arrays. However, like Gawk, BSD Awk also implements it for arrays, and so does Mawk since v1.3.4 (whereas Mawk v1.3.3 and below resulted in the error seen by the OP).
P.S.: To complement this elegant solution for looping over a sequentially indexed array with one for counting any array's elements: for (k in a) ++count.
5

I got this issue. Script worked on old server and stop to work on a new one.

Issue was that old server had 'gawk' installed, and new one has 'mawk', which does not support arrays.

I solved it by sudo apt-get install gawk.

1 Comment

I had this error on a Debian 8 server that had mawk installed. The issue was with length(array). See the other answer.
1

I did not see anything wrong with your script, as far as syntax goes. I saved your code in a file called script.awk, and executed:

awk -F: -f script.awk file1 file2

and did not see any error. Why don't you try the same: put your script in a separate file and invoke awk on it. If you still have the same problem, I suspect the problem might be in the data file.

Update

I cleaned up the code a little, the new version may be easier to read:

FNR==NR {a[FNR] = $1; next}

{
    for (i in a) {
        if (match($0, a[i])) {
            print a[i], $1
        }
    }
}

5 Comments

I tried nawk, 1 nawk -F: ' 2 FILENAME==ARGV[1] { 3 a[FNR]=$1 4 } 5 FILENAME==ARGV[2] { 6 for(i=1;i<=length(a);i++) { 7 if(match($0,a[i])) { 8 print a[i],$1 9 } 10 } 11 }' 16.passwd 16.group | sort got same problem: nawk: line 6: illegal reference to array a
It is very strange.If I am trying to use one line awk command, I got "awk: line 6: illegal reference to array a" error.But if I use the script.awk , then perform awk -F: -f script.awk 16.passwd 16.group. It works. Can anyone help me out of here? Thanks
It probably has something to do with quoting and the shell.
No, it's nothing to do with quoting and the shell, it's using a GNU awk extension when not using GNU awk.
How can I put $1,$3,$4 from in an array? awk -F: 'FILENAME==ARGV[1]{a[FNR]=$1};FILENAME==ARGV[2]{for(i=1;i in a;i++){if(match($0,a[i])){print a[i],$1}}}' file1 file2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.