Hi all,
I have a list in a text (.txt) file. I want to find out how many times each line occurs in the text file. For example, if the text file contained;
apple apple apple orange orange pear
I want to be able to read it and get;
apple 3 orange 2 pear 1
...at the moment, I'm doing this with the following script, and it works.
set theResult to "" set theFile to choose file set theContents to every paragraph of (do shell script "cat " & quoted form of (POSIX path of theFile))
--remove duplicates using LNS's List and Record OSAX set theNewList to difference of {} and theContents with removing duplicates
repeat with theItem in theNewList set theResult to theResult & theItem & "\t" & (do shell script "egrep -c " & quoted form of theItem & " " & quoted form of (POSIX path of theFile)) & "\r" end repeat
...but some of the text files are REALLY long, and it takes quite a while. I was wondering whether anyone had any clever ideas for doing this a bit more quickly. Perhaps a big chunk of "do shell script" would speed things up, but it's still early days for me and shell scripting.
Any ideas or suggestions appreciated!
Thanks Rob |