Jump to content

string search in a file


Ese
 Share

Recommended Posts

Hello

i'm trying with no success.. to do this

i have a simple txt file with usernames looks like this:

Ese

Blese

nasulin

roy

me

you

etc

and i have a log file with a lot of information.

i need to take the first user and see how many times it appears in the log file. and output it somehow.

take the second user and do the same and so on...

Any help would be appriciated

Cheers

Link to comment
Share on other sites

$File_1 = "Path\somefile.txt"
$File_2 = FileRead("Path\AnotherFile.txt")

$aArray = _StringToArray2($File_1)

For $i = 0 To Ubound($aArray)-1
   $sTest = StringReplace($File_2, $aArray[$i], $aArray[$i])
   MsgBox( 4096, "Results", "The name appeared " & @Extended & " times")
Next

Func _StringToArray2($sStr)
   If FileExists($sStr) Then $sStr = FileRead($sStr)
   If $sStr = "" Then Return SetError(1);; Empty strings not allowed
   Local $aRegEx = StringRegExp($sStr, "(?i)(.*)(?:\z|\r\n|\r|\n)", 3)
   If @Error Then Return SetError(2);; RegExp failed
   Return $aRegEx
EndFunc

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

a sample of the users file is above, the second file is just some normal log

I'm thinking of something:

$file = FileOpen("filename.txt", 0)

While 1
   $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Test:", $line) \\\ maybe insted of this line a code to search in the log file and return the times it appears...?
Wend

but not sure.

cheers

Link to comment
Share on other sites

FileReadLine is the slowest possible method of reading the contents of a file

The code I gave you reads it to an array first and then loops through the array using StringReplace("String", "Text", "Text") which equates to no change in the file because we just read the second file into a variable anyway, and uses the @Extended to find out how many times the string was "replaced" with the same string.

Edit: Here is a quick example of what would happen

$sStr = "Is Jos doing something that Jos shouldn't be doing? Have a great day doing it Jos."

StringReplace($sStr, "Jos", "Jos")

MsgBox (4096, "Results", "Jos was mentioned " & @Extended & " times in the string.")

;;@Extended would return 3 so the message box would read

;; Jos was mentioned 3 times in the string.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Here is a function that I have on my website that does exactly what I posted above.

;===============================================================================
; Function Name:   _StringGetChrCount()
; Description:   Count the number of times a given character appears in a string
; Syntax:   
; Parameter(s):   $cStr - The string to check
;                $cChr - The character to count
;
; Requirement(s):   
; Return Value(s):   The number of times a character appears in a string
; Author(s):   George (GEOSoft) Gedye
; Modification(s):   
; Note(s):   Can also be used to determine how many times a string appears in a file
;            by using FileRead() for $cStr and the string for $cChar
; Example(s): $Test = _StringGetChrCount("This is just an example.", Chr(32))
;            Since Chr(32) is a space then it would return 4
;===============================================================================

Func _StringGetChrCount($cStr, $cChr, $iCase = 0)
  If $iCase <> 0 Then $iCase = 1
  StringReplace($cStr, $cChr, $cChr, 0, $iCase)
  Return @Extended
EndFunc  ;<==> _StringGetChrCount()

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Actually this is even better because it will check word boundaries. Example: If the name was Jos then the origional would also count Joseph. This one won't do that

$File_1 = "Path\somefile.txt"
$File_2 = FileRead("Path\AnotherFile.txt")

$aArray = _StringToArray2($File_1)

For $i = 0 To Ubound($aArray)-1
   $sTest = StringRegExpReplace($File_2, "(?i)\b(" & $aArray[$i] & ")\b", "$1")
   MsgBox( 4096, "Results", "The name appeared " & @Extended & " times")
Next

Func _StringToArray2($sStr)
   If FileExists($sStr) Then $sStr = FileRead($sStr)
   If $sStr = "" Then Return SetError(1);; Empty strings not allowed
   Local $aRegEx = StringRegExp($sStr, "(?i)(.*)(?:\z|\r\n|\r|\n)", 3)
   If @Error Then Return SetError(2);; RegExp failed
   Return $aRegEx
EndFunc
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...