awk associative array with pattern as index

branquito picture branquito · Jun 3, 2013 · Viewed 11.5k times · Source

what would be the best solution to have awk store searched pattern along with lines where it was found in an array.. do i need a shell script for that or it can be done using only awk..

so for example if i search for word 'guitar', it makes an array which holds info that that word was found on line 13, 18 and 89 for example?

awk '/home/ {
array[$0] = NR 
}
END {
for(i in array) print i, array[i] }' 1-1000.txt

for example this would print lines that matched along with number where they were found.. but i need not $0 but that 'home' pattern, as index of associative array which would hold lines as values.. but then again there is problem how to have multiple values for that one index??

Answer

jaypal singh picture jaypal singh · Jun 3, 2013

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