Sign in to follow this  
Followers 0
mmletzko

Need help with StringInStr AND

21 posts in this topic

#1 ·  Posted (edited)

This is driving me nuts...cannot get this to work. Trying to do a search for 2 strings in a line and if it finds it, write that line to another file. Searching for either one of these strings by itself works fine, but when I combine them using AND its not working. "2009" would be part of a date formatted as something like "3/4/2009" - I'm only looking for entries from 2009. If I change the "2009" to another string that is alpha characters, the AND works fine. Is there something about a string being a number?

Select
Case StringInStr ($linesource, "xxx") AND StringInStr ($linesource, "2009")
$linedest = FileWriteLine ($logdest, $linesource)
Endselect
Edited by mmletzko

Share this post


Link to post
Share on other sites



mmletzko,

This works fine for me:

$linesource = "qwertxxxyuiop" & "3/4/2009"

Select
    Case StringInStr ($linesource, "xxx") And StringInStr ($linesource, "2009") 
        ConsoleWrite("Good to go" & @CRLF)
    Case Else
        ConsoleWrite("Ooops" & @CRLF)
EndSelect

Can you give an example of a typical $linesource variable? Are you sure the date is in the line as text characters?

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Thanks for replying so quickly Melba.

Here's a sample of the lines from the file:

3/5/2008,16:45:28,mmletzko,x.x.19.107,Administration session started

7/26/2007,16:45:46,pjones,x.x.19.107,Viewed "Reports & Activity - Administration Audit" report 'Administration Audit.csv'

6/14/2009,16:46:08,bsmith,x.x.19.107,Administration session finished

1/15/2009,11:41:07,mmletzko,x.x.19.107,Administration session started

10/15/2009,11:41:26,jjackson,x.x.19.107,Viewed "Reports & Activity - Administration Audit" report 'Administration Audit.csv'

Basically what I'm doing is scanning through all the lines of an export log. I want to find all the entries from a specific user during the year 2009.

If I search for "mmletzko" only, it works fine...I get all the entries that include mmletzko.

If I search for "2009" only, it works fine...I get all the entries that include 2009.

If I search for "mmletzko" AND "2009", I don't get any results.

If I search for "mmletzko" AND "Adminstration" (for example), it works fine.

Share this post


Link to post
Share on other sites

mmletzko,

Using the data you provided this works fine for me:

#Include <File.au3>
$iCount = _FileCountLines(@ScriptDir & "\line.txt")

$hFile = FileOpen(@ScriptDir & "\line.txt", 0)

For $i = 1 To $iCount
    
    $linesource = FileReadLine($hFile)

    ConsoleWrite($linesource & @CRLF)

    Select
        Case StringInStr ($linesource, "mmletzko") And StringInStr ($linesource, "2009") 
            ConsoleWrite("Good to go" & @CRLF)
        Case Else
            ConsoleWrite("Ooops" & @CRLF)
    EndSelect
Next

FileClose($hFile)

Result:

3/5/2008,16:45:28,mmletzko,x.x.19.107,Administration session started
Ooops
7/26/2007,16:45:46,pjones,x.x.19.107,Viewed "Reports & Activity - Administration Audit" report 'Administration Audit.csv'
Ooops
6/14/2009,16:46:08,bsmith,x.x.19.107,Administration session finished
Ooops
1/15/2009,11:41:07,mmletzko,x.x.19.107,Administration session started
Good to go
10/15/2009,11:41:26,jjackson,x.x.19.107,Viewed "Reports & Activity - Administration Audit" report 'Administration Audit.csv'
Ooops

Does it still fail for you?

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

mmletzko,

Using the data you provided this works fine for me:

#Include <File.au3>
$iCount = _FileCountLines(@ScriptDir & "\line.txt")

$hFile = FileOpen(@ScriptDir & "\line.txt", 0)

For $i = 1 To $iCount
    
    $linesource = FileReadLine($hFile)

    ConsoleWrite($linesource & @CRLF)

    Select
        Case StringInStr ($linesource, "mmletzko") And StringInStr ($linesource, "2009") 
            ConsoleWrite("Good to go" & @CRLF)
        Case Else
            ConsoleWrite("Ooops" & @CRLF)
    EndSelect
Next

FileClose($hFile)

Result:

3/5/2008,16:45:28,mmletzko,x.x.19.107,Administration session started
Ooops
7/26/2007,16:45:46,pjones,x.x.19.107,Viewed "Reports & Activity - Administration Audit" report 'Administration Audit.csv'
Ooops
6/14/2009,16:46:08,bsmith,x.x.19.107,Administration session finished
Ooops
1/15/2009,11:41:07,mmletzko,x.x.19.107,Administration session started
Good to go
10/15/2009,11:41:26,jjackson,x.x.19.107,Viewed "Reports & Activity - Administration Audit" report 'Administration Audit.csv'
Ooops

Does it still fail for you?

M23

There is a logic bomb in here. StringInStr() returns the integer character position where the match string was found. In you example, the fourth line returns 20 and 6 (the posistions of "mmletzko" and "2009"). These values are not boolean, and when the "AND'd" together, you get:
(00010100) AND (00000110) = 00000100
This works for your demo, but only by accident, because 20 AND 6 = non-zero (true). If the integer values happen to AND into zero (i.e. 24 AND 6 = 0) then you get boolean FALSE even though both strings are present. The test should be:
Case (StringInStr ($linesource, "mmletzko") <> 0) And (StringInStr ($linesource, "2009") <> 0)
The compare operators will give boolean results as expected.

:party:

Edit: I was wrong. (Don't understand it, never happened before.) I didn't bother to code a demo for what I was talking about, just bit'ed out the numbers on paper. I should have realized I was confusing using the AND operator with using the BitAND() function. Should have tested before posting:

For $x = 0 To 3
    For $y = 0 To 3
        ConsoleWrite($x & " AND " & $y & " = " & ($x AND $y) & @LF)
        ConsoleWrite($x & " BitAND " & $y & " = " & BitAND($x, $y) & @LF)
    Next
Next

Note for example where $x = 1 and $y = 2, the AND = TRUE, but the BitAND = 0 (FALSE). So the StringInStr() AND StringInStr() check should have worked in all cases. Sorry for the distraction, I'll shut up now...

:)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Correct, Melba. And is a logical boolean and, while BitAnd will perform the bitwise and.

Share this post


Link to post
Share on other sites

I'll include my whole code so you can see if I'm missing something:

$logsource = FileOpen ("c:\temp\sourcefile.csv", 0)
While 1
$linesource = FileReadLine ($logsource)
if @error = -1 then ExitLoop
 $logdest = FileOpen ("c:\temp\destinationfile.csv",1)
 Select
 Case StringInStr ($linesource, "mmletzko") AND StringInStr ($linesource, "2009) 
    $linedest = FileWriteLine ($logdest, $linesource)
 Endselect
 FileClose($logdest)
Wend
FileClose ($logsource)

Share this post


Link to post
Share on other sites

Thanks Salty...I'll check that out and see if it works. I was wondering if I'd have to specify the result of the InString but hadn't actually tried it.

Share this post


Link to post
Share on other sites

What is the point of "$linedest"?

Share this post


Link to post
Share on other sites

What is the point of "$linedest"?

That may have been left over from when I was checking the result of that operation or something. Guess I can get rid of it now.

Share this post


Link to post
Share on other sites

Try using a regular If statement instead of Select. I don't know why you have select in there anyways with only one option.

Share this post


Link to post
Share on other sites

Try using a regular If statement instead of Select. I don't know why you have select in there anyways with only one option.

Its because I didn't add the other statements to the forum...didn't feel it was necessary to put the same code in there when its just checking for different names or dates, etc.

Unfortunately the code suggested doesn't seem to be working either:

Case (StringInStr ($linesource, "mmletzko") <> 0) And (StringInStr ($linesource, "2009") <> 0)

I'm getting nothing for the results like my original issue. I haven't detailed the results yet...need to do something else for a little while...so if anyone has any ideas I'm all ears!!

Thanks again guys...

Share this post


Link to post
Share on other sites

mmletzko,

Are you saying that you do NOT get the same results as I did when you save the data you provided as a separate text file? It may be that the file you are reading (I assume it is a spreadsheet/database given the .csv extension) saves the date as something other than a literal string - which would make the StringInStr useless.

Did you extract those lines directly from the file, or did you get them by some other route? Could you post an actual file - it might offer some idea of what is happening internally.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

mmletzko,

Are you saying that you do NOT get the same results as I did when you save the data you provided as a separate text file? It may be that the file you are reading (I assume it is a spreadsheet/database given the .csv extension) saves the date as something other than a literal string - which would make the StringInStr useless.

Did you extract those lines directly from the file, or did you get them by some other route? Could you post an actual file - it might offer some idea of what is happening internally.

M23

What I mean is when I use the line suggested by Salty, checking for the results of each StringInStr being <> 0, I still get no results in the destination log file.

The application I'm exporting it from (CiscoSecure ACS) just has the .csv option, but don't think there's anything special about it because if I change the StringInStr statement to check for ONLY "2009" (no AND operator), it correctly returns all the lines that include "2009".

The lines I posted earlier is exactly what the file looks like...there's just 100/1000s of lines.

Share this post


Link to post
Share on other sites

Could always go with:

Case StringInStr ($linesource, "mmletzko") * StringInStr ($linesource, "2009")

Share this post


Link to post
Share on other sites

Could always go with:

Case StringInStr ($linesource, "mmletzko") * StringInStr ($linesource, "2009")

Thanks for the suggestion Spiff...unfortunately this doesn't work either. :)

Share this post


Link to post
Share on other sites

$t = "3/5/2008,16:45:28,mmletzko,x.x.19.107,Administration session started" & @CRLF & _
"7/26/2007,16:45:46,pjones,x.x.19.107,Viewed " & '"Reports & Activity - Administration Audit"' & " report 'Administration Audit.csv'" & @CRLF & _
"6/14/2009,16:46:08,bsmith,x.x.19.107,Administration session finished" & @CRLF & _
"1/15/2009,11:41:07,mmletzko,x.x.19.107,Administration session started" & @CRLF & _
"10/15/2009,11:41:26,jjackson,x.x.19.107,Viewed " & '"Reports & Activity - Administration Audit"' & " report 'Administration Audit.csv'"

$aLines = StringSplit($t, @CRLF, 1)
For $i = 1 To $aLines[0]
    If StringInStr($aLines[$i], "mmletzko") > 0 And StringInStr($aLines[$i], "2009") > 0 Then _
        ConsoleWrite($aLines[$i] & @LF)
Next

Share this post


Link to post
Share on other sites

Thanks for the suggestion Spiff...unfortunately this doesn't work either. :)

Hmm, if either StringInStr() returns a zero, the multiplication would result in a zero, or a boolean FALSE.

It ought to work...

It works with the example line.txt you posted initially.

What does a line that doesn't work look like?

Share this post


Link to post
Share on other sites

mmletzko,

My last throw of the dice. If it works for you when the 2 tests are isolated, see if it works when they are separated!:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
$logsource = FileOpen(@ScriptDir & "\line.txt", 0)

While 1
    $linesource = FileReadLine($logsource)
    If @error = -1 Then ExitLoop
    $logdest = FileOpen(@ScriptDir & "\destinationfile.txt", 1)
    Select
        Case StringInStr($linesource, "mmletzko") <> 0
            If StringInStr($linesource, "2009") <> 0 Then $linedest = FileWriteLine($logdest, $linesource)
    EndSelect
    FileClose($logdest)
WEnd

FileClose($logsource)

You will have to change the file names back to your versions, of course.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Did you have a look with a HexViewer to see if the file is maybe a UNICODE file?

this should work and all other suggested variations are really the same thing:

Case StringInStr ($linesource, "mmletzko") AND StringInStr ($linesource, "2009")

Maybe yopu could post and extract of the file with only a couple of records in it that should work and isn;'t working for you?


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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