Using awk instead of grep -f

Hi Guys.
I am trying to count occurances of patterns(occurance can be anywhere in file2) from file1 in file2.
file1 is

god
god
pod 
rod
file2 is
iamgod
iamgod
podrod
123rod456

output should be

god 2
god 2
pod 1
rod 2

I am not good at awk but i figured out this command.it doesnt give me desired output.

awk -F "god" '{s+=(NF-1)} END {print s}' file2

Can someone help please.
Thank you
Sam

--- Post updated at 09:09 AM ---

i also have this awk script but its not giving me desired output

#!/usr/bin/awk -f
FNR==NR {
  f2[$0]
  next
}
{
  for(i in f2) {
    if($0 ~ (i))
      beg++
    if($0 ~ (i"$")) {
      end++
  }
}
}
END {
    print "anywhere"
    for(i in f2)
       print i,beg+0
}

Hey.

What OS and shell are you using?

Debian bash

Would this come close?

grep -offile1 file2 | sort | uniq -c
      2 god
      1 pod
      2 rod

--- Post updated at 16:24 ---

Hoppla - didn't take your thread title into account. Try this awk :

awk '
FNR==NR {OUT[NR] = $1
         CNT[$1]
         MX = NR
         next
        }
        {for (c in CNT) if (match ($0, c)) CNT[c]++
        }
END     {for (i=1; i<=MX; i++) print OUT, CNT[OUT]
        }
' file[12]
god 2
god 2
pod 1
rod 2
2 Likes

Thanks,
i think this works.testing it for a big file.
just one thing.
i am trying to put the codes in a .awk file and then run it.
i would like to run it like

perl script.awk input1 input2 >output

how can i do this?

perl ?

sorry
i meant awk

awk -f  script.awk file1  file2>output

can i mention the file1 and file2 using above command.
could you modify the script you gave me?
thanks

Put the part between but excluding the single quotes into a file, name it to taste, and run it as you posted. file[12] will be expanded b the shell to file1 file2 .