Make grep -c display like grep -n?

Hey Guys,

Wondering if there is a way to do the following

I have a file called test.txt

abc
def
abc
abc
def

I have a pattern file called pattern.txt containing the following

abc
def

I want to do a count, but have it display the count value preceeding each line like grep -n

so

grep -n -f pattern.txt would show

1:abc
2:def
3:abc
4:abc
5:def

What I would like to do is have grep count the number of occurances in the file and place that value at the begining of the output so it would look like

3:abc
2:def

3 means abc occured 3 times in the file
2 means def occured 2 times in the file

Is there a function or a clever way to achieve this?

Thanks in Advance

I don't know if you could do this without reading the file line-by-line, but here goes:

while read pattern; do
   count=$(grep -c $pattern test.txt); 
   echo $count:$pattern; 
done < pattern.txt

Can't get grep to do all the work but a quick little script can do it

Say the script name is showdetails.sh

Usage;
showdetails /path_to/pattern.file /path_to/test.txt

Inside the script....

#!/usr/bin/sh

count=1
for nxt in `cat $1`
do
out[$count]="`grep -c $nxt $2`:$nxt\n"
count=`expr $count + 1`
done
echo ${out[@]} | tr -d "[:blank:]"

XX - END SCRIPT --

The tr will remove the blanks in the out put to get a nice neat colum, try it with the tr function and see.

That should work, if my typing is ok...

Goot go and do some firm upgrades in linux.... wish me luck..