Sign in to follow this  
Followers 0
maxcronjob

Count number of occurrances of a word in a file...

5 posts in this topic

Okay,

I created a monster script that does a lot of testing and prints out lines to a log file. The log file has a number of PASS and FAIL entries in it. What I need to do is come up with a way to count all the PASS and all the FAIL entries and spit them out at the END of that logfile...

I'm wondering if there's a way to do this outside the script rather than going back in and adjusting each PASS and FAIL to increase in count after they've printed out to the log file, then print out the number of counts for each in the end.

Any suggestions?

thanks,

max

Share this post


Link to post
Share on other sites



_FileReadToArray() would be a good start.

Loop through the array and use StringSplit() if there are multiple entries per line.

Increment your counters whenever you find a match.

p.s.

Doing this within the monster script would be much quicker.


[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

#include <file.au3>
Dim $aRecords, $search = "Line3"
If Not _FileReadToArray(@ScriptDir & "\test.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading file to Array    error:" & @error)
   Exit
EndIf
$y = 1
$occurance = 0
For $x = 1 to $aRecords[0]
    while 1
        if StringInStr($aRecords[$x],$search,0,$y) Then
            $occurance = $occurance + 1
            $y = $y + 1
        Else
            $y = 1
            ExitLoop
        EndIf
    WEnd
Next
Msgbox(0,'found word (' & $search & ')',$occurance & " times.")

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

#include <file.au3>
Dim $aRecords, $search = "Line3"
If Not _FileReadToArray(@ScriptDir & "\test.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading file to Array    error:" & @error)
   Exit
EndIf
$y = 1
$occurance = 0
For $x = 1 to $aRecords[0]
    while 1
        if StringInStr($aRecords[$x],$search,0,$y) Then
            $occurance = $occurance + 1
            $y = $y + 1
        Else
            $y = 1
            ExitLoop
        EndIf
    WEnd
Next
Msgbox(0,'found word (' & $search & ')',$occurance & " times.")

AWESOME! I tweaked it and got it to work! PERFECT!!!! THANKS!!!!!!

max

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0