awk Search Array Element Return Index

Can you search AWK array elements and return each index value for that element.

For example an array named car would have index make and element engine. I want to return all makes with engine size 1.6.

Array woulld look like this:

BMW 1.6
BMW 2.0
BMW 2.5
AUDI 1.8
AUDI 1.6

Output I want is:

BMW
AUDI

Try this:

awk '{if ($2 == 1.6 ) { print $1} }' inputFile

Or this

awk '$2 ~ /1\.6/ { print $1}' inputFile

I don't think you can have that array as the index needs to be unique. One possible index could be "make & engine". Then you could try sth like (untested)

for (i in array) {split (i, M); X[M[1]]} for (i in X) print i}

Agreed - stupid example. Was trying to make it simple.

Real world - we have two arrays (source & comparison). What I want is for each source index element value return all the element matching comparison index values.

Source:
102030 BEE77DA5BE9F24FBE044002128B2E77C

Comparison:
102030 BEE77DA5BE9F24FBE044002128B2E77C
102040 BEE77DA5BE9F24FBE044002128B2E77C
102050 AEE77123402128B2EF24FBEE77D28B2E
102080 BEE77DA5BE9F24FBE044002128B2E77C

Results:
Source: 102030
Result: 102040
Result: 102080

Here is an example performing array value comparison.

awk '
        BEGIN {
                SRC["102030"] = "BEE77DA5BE9F24FBE044002128B2E77C"
                CMP["102030"] = "BEE77DA5BE9F24FBE044002128B2E77C"
                CMP["102040"] = "BEE77DA5BE9F24FBE044002128B2E77C"
                CMP["102050"] = "AEE77123402128B2EF24FBEE77D28B2E"
                CMP["102080"] = "BEE77DA5BE9F24FBE044002128B2E77C"
        }
        END {
                print "Results:"
                for ( v in SRC )
                {
                        for ( k in CMP )
                        {
                                if ( k == v )
                                        print "Source: " k
                                else if ( CMP[k] == SRC[v] )
                                        print "Result: " k
                        }
                }
        }
' /dev/null

Modify as per you requirement.

Thank you. Was struggling with syntax.

If you don't initialise v it means loops through all records.

for ( v in SRC )

Yes, there is no need to initialize variable: v

It is a special kind of for statement for scanning an array.

Struggling to obtain the correct output format.

What I want is (problem name and hash values should line up):

Source_Name Problem_Name Hash
102030         102080           BEE77DA5BE9F24FBE044002128B2E77C
                   102040           BEE77DA5BE9F24FBE044002128B2E77C

Amended script:

nawk -F= '
        BEGIN {
                SRC["102030"] = "BEE77DA5BE9F24FBE044002128B2E77C"
                CMP["102030"] = "BEE77DA5BE9F24FBE044002128B2E77C"
                CMP["102040"] = "BEE77DA5BE9F24FBE044002128B2E77C"
                CMP["102050"] = "AEE77123402128B2EF24FBEE77D28B2E"
                CMP["102080"] = "BEE77DA5BE9F24FBE044002128B2E77C"
        }
        END {
                printf "Source_Name" " Problem_Name Hash\n"
                for ( v in SRC )
                {
                        for ( k in CMP )
                        {
                               if ( k == v )
                                        printf k
                               else if ( CMP[k] == SRC[v] )
                                        printf "%+13s\n",k CMP[k]
                        }
                }
        }
' /dev/null

Output:

Source_Name Problem_Name Hash
102030102080BEE77DA5BE9F24FBE044002128B2E77C
102040BEE77DA5BE9F24FBE044002128B2E77C

modify

else if ( CMP[k] == SRC[v] )
                                        printf "%+13s\t%s\n",k,CMP[k]

Separators were missing

Almost, 102040 should line up with 102080.

Source_Name Problem_Name Hash
102030      102080      BEE77DA5BE9F24FBE044002128B2E77C
      102040    BEE77DA5BE9F24FBE044002128B2E77C

Maybe I'm going about it the wrong way. How would I set a fixed starting point for:

printf "%+13s\t%s\n",k,CMP[k]

Try

        for ( k in CMP )
                {printf "%13s\t", (k==v)?k:""
                 if ( CMP[k] == SRC[v] )
                        printf "%+13s\t%s\n",k,CMP[k]
                }

Not quite but I can see where you're going. I'll play about with it a bit.

Source_Name Problem_Name Hash
      102030          102030    BEE77DA5BE9F24FBE044002128B2E77C
                      102080    BEE77DA5BE9F24FBE044002128B2E77C
                                      102040    BEE77DA5BE9F24FBE044002128B2E77C

---------- Post updated at 02:38 PM ---------- Previous update was at 01:59 PM ----------

Sod it. Made it easier.

for ( k in CMP )
                        {
                              if ( CMP[k] == SRC[v] )
                                  printf ("%-12s%-13s%s\n",v,k,CMP[k]) 
                        }