1

I have an awk script with the lines below:

NR==FNR {
    if (FNR>1) {
        a[$3][++i]=$6
        b[$3][i]=$7
    }
    next
}

When i use the script it says

awk: syntax error at source line 3 source file ../Overlap.awk
 context is
        >>>  a[$3][ <<<  
awk: illegal statement at source line 3 source file ../Overlap.awk

The script is copied from a linux machine to MAC OSX and the problem arised. Could someone help to fix this.

1

1 Answer 1

2

AWK doesn't support multidimensional arrays the way most languages do, or at least the way we're used to. Here is how you can access indexes in associative arrays using AWK:

NR==FNR {
    if (FNR>1) {
        a[$3, ++i]=$6
        b[$3, i]=$7
    }
    next
}

You can read more about AWK and multidimensional arrays here and here.

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

3 Comments

but it worked in linux machine and i wonder why doesn't it work in MAC OSX
@user1779730: In general, different platforms use different flavors of awk that differ in behavior and features. For instance, OSX uses FreeBSD awk, while some Linux flavor use gawk, while other use mawk. However: All flavors mentioned do NOT support the separate-subscripts syntax; it's also not in the POSIX spec (pubs.opengroup.org/onlinepubs/009696699/utilities/awk.html); even if there is a flavor out there where that works - please tell us what it is- you should avoid this syntax for the sake of portability.
Yes this is a good advice, also note that it is possible to install Gnu Awk version 4 on OSX as described in this thread groups.google.com/forum/#!topic/comp.lang.awk/QM51VPAlw4I

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.