3

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??

branquito
  • 3,864
  • 5
  • 35
  • 60

1 Answers1

5

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
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • it's that i am using list like yours, and then for each line as a pattern i would like to remember line where it was found in second file.. that second file would have for example line that says monkeydeal, and the monkey from this first list would match that and remember the line it was found on in 2nd file.. thanks for the first solution because that would be useful in bash script, passing monkey as a regex to pull out line numbers – branquito Jun 03 '13 at 02:02
  • so that 'home' from my example is comming from 1st file, and that 1-1000.txt is file i am matching against to see the lines where its contained – branquito Jun 03 '13 at 02:07
  • @branquito I added an update to the answer. Hope that helps. Good luck – jaypal singh Jun 03 '13 at 02:12
  • that's great, but how would i have from there numbers of lines where it was found, so it should say monkeydeal found on line 1 because of --> monkey, apple is a good fruit found on line 3 because of -->apple (line numbers should come from 2nd file, so in this example text2) thanks! – branquito Jun 03 '13 at 02:40
  • @branquito That is easy. Use `FNR` which stores line number for each file. In this case it would be line number of second file. – jaypal singh Jun 03 '13 at 02:43