(1) Glad to see that you sent your message directly to my mailbox. I didn’t received it thru the list.
set leFichier to (path to desktop as text) & "forGrep.txt"
set appleCount to my countOfKeyInSource(leFichier, " apple ")
set grapeCount to my countOfKeyInSource(leFichier, "grape")
on countOfKeyInSource(theSource, theKey)
return do shell script "grep " & theKey & space & (quoted form of POSIX path of theSource) & "|wc -w"
end countOfKeyInSource
And of course it give the same puzzling results than the original version.
When the occurrences are only text ones, the result is OK.
When an occurrence is just before a number, for instance "apple 3" it's counted twice.
I don’t know if such case may exist in the original asker's datas but, at least for me, it's SURPRIZING.
The new message posted by Omar K N pushed me in an interesting direction :
when an occurrence is available in a paragraph containing x "words", it's counted x times.
With this text
-----------
a0 apple a2 a3 a4 a5 a6
grape 1
banana 1
ananas 1
orange 1
apple
apple
orange 2
--------------
the result is :
apple 9
grape 2
I’m sure that it’s not the wanted result.
After reading your late message (received thru the list), I tried to use " apple " as the key and got exactly the same results.
Yvan KOENIG (VALLAURIS, France) lundi 8 septembre 2014 18:25:51