Count number of times a string is in a text file

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
mikeyd03
Newbie
Posts: 9
Joined: Tue Sep 14, 2004 2:27 pm

Count number of times a string is in a text file

Post by mikeyd03 » Wed Mar 16, 2005 11:02 pm

I'm a newbie here, but I'm trying to use Readfile to count the number of times the word ON is in a text file, IF the number is >2, then do something. This is further complicated by the fact that the word NONE is in the file, so I really need to count ON, if you get my drift.

Any help?

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Mar 17, 2005 6:29 am

A couple of weeks ago I posted a sample that might assist you in your cause. See the post at:

http://www.mjtnet.com/usergroup/viewtopic.php?t=1712

In your case, use ON as the delimiter for the "separate" command to split up the results of "readfile". Notice that "returnvar_count" will be one greater than the number of occurances of ON.

Hope this helps,
Dick

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Thu Mar 17, 2005 4:57 pm

If "ON" can ever be at the beginning or end of a line then there won't always be a space as the previous/next character.

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Thu Mar 17, 2005 4:58 pm

If "ON" can ever be at the beginning or end of a line then there won't always be a space as the previous/next character.

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Thu Mar 17, 2005 5:17 pm

Use regular expressions:

VBSTART
Function CountPatterns(patrn, strng)
Dim regEx, Match, Matches
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(strng)
CountPatterns = Matches.Count
End Function
VBEND
Let>WordToCount=ON
Let>MyString=ON AND ON AND ON NONE
VBEval>CountPatterns("\b%WordToCount%\b","%MyString%"),numOns

In the example above numOns will equal three as there are three distinct instances of the word ON. Note that it ignores the ON inside NONE because that is not a separate word. The \b \b around the word to find tells it to look for distinct instances.
MJT Net Support
[email protected]

mikeyd03
Newbie
Posts: 9
Joined: Tue Sep 14, 2004 2:27 pm

Post by mikeyd03 » Thu Mar 17, 2005 7:46 pm

Support, that may be what I need. How can I have MyString contain an entire text file I tried this, but got an error:

VBSTART
Function CountPatterns(patrn, strng)
Dim regEx, Match, Matches
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(strng)
CountPatterns = Matches.Count
End Function
VBEND
Let>WordToCount=ON
ReadFile>C:\Documents and Settings\miked\Desktop\TEST.TXT,MyString
//Let>MyString=ON AND ON AND ON NONE
VBEval>CountPatterns("\b%WordToCount%\b","%MyString%"),numOns
Message>%numOns%

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Thu Mar 17, 2005 8:13 pm

For me it works OK with a single line file but with a multiple lines in the file I get Microsoft VBScript Compilation error :1033 Unterminated string constant, line 2, column 24

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Fri Mar 18, 2005 12:01 am

//Remove CRLFs and Quotes
StringReplace>MyString,CRLF,,MyString
StringReplace>MyString,",,MyString

VBEval>CountPatterns("\b%WordToCount%\b","%MyString%"),numOns
MJT Net Support
[email protected]

mikeyd03
Newbie
Posts: 9
Joined: Tue Sep 14, 2004 2:27 pm

Post by mikeyd03 » Fri Mar 18, 2005 9:46 pm

Thanks, support. Seems to work fine now!

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts