find/grep two items and display both

I have a file against which I can grep a string for. I can also check for that string count using wc -l (or grep -c). I need to display the results of both in one output i.e. 'line containing string' and 'count' - what would be the most efficient way of managing this? Thanks in advance.

Try...

awk '/string/{print "line=" NR, "count=" ++cnt}' file1

Please post what Operating System and version you are running and what Shell you use.

Assuming some sort of modern Bourne-type Shell:

Not particularly efficient (because it reads the input file three times), but using commands mentioned in post #1.

filename="/path/name_of_file"
string="search string"
found=`grep -l "${string}" "${filename}"`
if [ ! "${found}""X" = "X" ]
then
          grep "${string}" "${filename}"
          count=`grep -c "${string}" "${filename}"`
          echo "count = ${count}"
fi

There is a better solutions using a workfile to hold the hits and then counting the hits.

thank you to both forum members who responded - much appreciated. I will try the solution today.

---------- Post updated at 09:55 AM ---------- Previous update was at 09:54 AM ----------

Also, I need to be able to use it on either Red Hat/Ubuntu (I can use any shell provided the solution works). Once again, thank you for looking into this.