It is important to know that keys
are unique. So, if you intend to store search pattern as key and line number as value then value will get overwritten by the last line the pattern was seen.
So a good way to do this would be:
awk '{a[NR]=$1} END {for (k in a) if(a[k]=="monkey") print k}' textile
Output:
[jaypal:~] cat textfile
monkey
donkey
apple
monkey
dog
cat
mat
horse
monkey
[jaypal:~] awk '{a[NR]=$1} END {for (k in a) if(a[k]=="monkey") print k}' textfile
4
9
1
If you need to iterate over line to look for a particular pattern and store it then you can use the for loop
to inspect each word of the line and once your word is found store that as an array.
awk '{ for (i=1;i<=NF;i++) if($i=="pattern") arry[NR]=$i } END {. . .}' inputfile
Update based on comments:
To iterate over two files (where one is being used as lookup and second to search for lines matching lookups).
awk 'NR==FNR{a[NR]=$1; next} {for (x in a) if ($0 ~ a[x]) print $0 " found because of --> " a[x]}' textile text2
Test:
[jaypal:~] cat text2
monkeydeal
nodeal
apple is a good fruit
[jaypal:~] awk 'NR==FNR{a[NR]=$1; next} { for (x in a) if ($0 ~ a[x]) print $0 " found on line number " FNR " because of --> " a[x]}' textfile text2
it is a good monkeydeal found on line number 1 because of --> monkey
it is a good monkeydeal found on line number 1 because of --> monkey
it is a good monkeydeal found on line number 1 because of --> monkey
apple is a good fruit found on line number 3 because of --> apple