Sign in to follow this  
Followers 0
steelsking

search in TAB delimited not working

22 posts in this topic

good day mates,

i have attached the log file.

these codes do not bring up the search which i intend to do.

can someone pls help me here?

$Found = "No"
$TxtFile = "log2.txt"
$data = FileRead($TxtFile)

If @error Then
    MsgBox(48, "Error!", "File could not be opened. " & @CRLF _
             & "1. Enter a valid path" & @CRLF _
             & "2. Check if the file exists" & @CRLF _
             & "3. Make sure you have access to the file" & @CRLF _
             & "4. Check if the file is not opened by another program/script")
    Exit
EndIf

$data = StringSplit($data, @CRLF, 1)

For $i = 1 To $data[0]
    $Line = $data[$i]
    $array = StringSplit($Line, @TAB)
    If $array[0] > 4 And StringInStr($array[3], "Local7.Warning") Then
        $Found = "Yes"
        MsgBox(0, "", $array[3])
    EndIf
Next

If $Found = "No" Then
    MsgBox(0, "Not found!", "AutoIt could not find the string you were searching for.")
EndIf

log2.txt

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Why you want to split each line at tab? If StringInStr($TxtFile, "Local7.Warning") then $Found = "Yes" should be enough or?

$TxtFile = "log2.txt"
$search = "Local7.Warning"
$data = FileRead($TxtFile)
If StringInStr($data, $search) Then
    $count = StringReplace($data, $search, $search)
    MsgBox(0, "Information", "Found '" & $search & "' " & @extended & "x in file " & $TxtFile)
Else
    MsgBox(0, "Not found!", "AutoIt could not find the string you were searching for.")
EndIf

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

firstly, i need a For loop to go through the whole file.

secondly, i need to split at the @TAB as i need to pull out the 4th array of the found line.

thanks for helping out.

Share this post


Link to post
Share on other sites

#include <file.au3>
#include <Array.au3>

Dim $aRecords
If Not _FileReadToArray("log.txt", $aRecords) Then
    MsgBox(4096, "Error", "Error reading log to Array error: " & @error)
    Exit
EndIf
For $x = 1 To $aRecords[0]
    If StringInStr($aRecords[$x], 'Local7.Warning') <> 0 Then
        MsgBox(0, "", $aRecords[$x])
    EndIf
Next
Exit

ok, i have the above codes. it is working though. but how to i get the 4th array of the msgbox result?

thanks.

Share this post


Link to post
Share on other sites

hi steelsking,

Your descriptions are somewhat ambiguous, however, hope this helps:

#Include <Array.au3>

$i=0
$file = FileOpen(@ScriptDir&"\log2.txt", 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $i+=1
    $lineArr=StringSplit($line,@TAB)
    If ($lineArr[2]="Local7.Warning") Then
        _ArrayDisplay($lineArr,"Warning found on line:"&$i)
    EndIf
Wend
FileClose($file)

Share this post


Link to post
Share on other sites

hi steelsking,

but how to i get the 4th array of the msgbox result?

It seems that perhaps you meant "How do you display the array's 4th element in a MsgBox()?", this can be done by specifying the elements' index in square brackets next to the variable.

Example:

$Array[0] ;accesses the first element of a zero-based array.
If you find youself in difficulty while working with arrays, this Arrays in AutoIt tutorial may be of some help.:unsure:

-smartee

Share this post


Link to post
Share on other sites

thank you so much smartee.

your codes made me eventually figure this out....

MsgBox(0, "", $lineArr[4])

thanks again!

Share this post


Link to post
Share on other sites

sorry one more question.

1183: 37w6d: %LINK-4-ERROR: FastEthernet1/1 is experiencing errors

2008 Jan 14 02:27:01 SIN +08:00 %CDP-4-NVLANMISMATCH:Native vlan mismatch detected on port 3/6

how do i read/filter the text starting from the last ":"

from the above to the following:

FastEthernet1/1 is experiencing errors

Native vlan mismatch detected on port 3/6

thanks!

Share this post


Link to post
Share on other sites

hi steelsking,

Glad I could help, and no problem, what you need is the StringRegExp() function. You can implement it like this:

#include <Array.au3>

$i = 0
$file = FileOpen(@ScriptDir & "\log2.txt", 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $i += 1
    $lineArr = StringSplit($line, @TAB)
    If ($lineArr[2] = "Local7.Warning") Then
        _ArrayDisplay($lineArr, "Warning found on line:" & $i)
        $extract = StringRegExp($lineArr[4], "%(?:[^:]+):(.*)", 1)
        _ArrayDisplay($extract, "Extracted portion")
    EndIf
WEnd
FileClose($file)

For additional information on the StringRegExp() function, see the helpfile. Good luck :unsure:

-smartee

Share this post


Link to post
Share on other sites

thanks a million!

appreciate it smartee!

Share this post


Link to post
Share on other sites

sorry to trouble again.

why doesn't the search and match work?

#include <Array.au3>
#include <File.au3>

Global $inifile = @ScriptDir & "\settings.ini"
Global $FileLocationName = IniRead($inifile, "config", "FileName", "FileName")
Global $Temp = @ScriptDir & "\tempdb.txt"
Global $extract

If Not FileExists(@ScriptDir & "\settings.ini") Then
    $Set = FileInstall("set.ini", @ScriptDir & "\settings.ini", 1)
    If $Set = 0 Then MsgBox(0, "Error", "Unable to copy default settings.ini")
    MsgBox(0, "Missing configuration file restored", "Please update the default settings.ini before proceeding.")
    $NP = Run("notepad.exe " & @ScriptDir & "\settings.ini", "", @SW_MAXIMIZE)
    If $NP = 0 Then MsgBox(0, "Error", "Unable to open settings.ini file for modification.")
    Exit
EndIf

$Search = IniRead($inifile, "config", "Search", "Search")
$i = 0
$file = FileOpen($FileLocationName, 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $i += 1
    $lineArr = StringSplit($line, @TAB)
    If ($lineArr[2] = $Search) Then
        ;_ArrayDisplay($lineArr, "Warning found on line: " & $i)
        $extract = StringRegExp($lineArr[4], "%(?:[^:]+):(.*)", 1)
        ;_ArrayDisplay($extract, "Extracted portion")
        ;MsgBox(0, "", $extract[0])

        Dim $aRecords
        If Not _FileReadToArray($Temp, $aRecords) Then
            MsgBox(4096, "Error", "Error reading log to Array error: " & @error)
            Exit
        EndIf
        For $x = 1 To $aRecords[0]
            ;MsgBox(0, 'Record:' & $x, $aRecords[$x])
            _ArrayToString
            If ($aRecords[$x] = $extract[0]) Then
                MsgBox(0, "", "Match")
            Else
                FileWrite($Temp, $extract[0] & @CRLF)
            EndIf
        Next
    EndIf
WEnd
FileClose($file)
Exit

thanks.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

hi again steelsking,

Its no trouble at all :unsure: The function _ArraySearch() should simplify things a bit for you, try this:

#include <Array.au3>
#include <File.au3>

Global $inifile = @ScriptDir & "\settings.ini"
Global $FileLocationName = IniRead($inifile, "config", "FileName", @ScriptDir & "\log2.txt")
Global $Temp = @ScriptDir & "\tempdb.txt"
Global $extract

If Not FileExists(@ScriptDir & "\settings.ini") Then
    $Set = FileInstall("set.ini", @ScriptDir & "\settings.ini", 1)
    If $Set = 0 Then MsgBox(0, "Error", "Unable to copy default settings.ini")
    MsgBox(0, "Missing configuration file restored", "Please update the default settings.ini before proceeding.")
    $NP = Run("notepad.exe " & @ScriptDir & "\settings.ini", "", @SW_MAXIMIZE)
    If $NP = 0 Then MsgBox(0, "Error", "Unable to open settings.ini file for modification.")
    Exit
EndIf

$Search = IniRead($inifile, "config", "Search", "Local7.Warning")
$i = 0
$file = FileOpen($FileLocationName, 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $i += 1
    $lineArr = StringSplit($line, @TAB)
    If ($lineArr[2] = $Search) Then
        $extract = StringRegExp($lineArr[4], "%(?:[^:]+):(.*)", 1)
        Dim $aRecords
        If Not _FileReadToArray($Temp, $aRecords) Then
            MsgBox(4096, "Error", "Error reading log to Array error: " & @error)
            Exit
        EndIf
        If (_ArraySearch($aRecords, $extract[0]) = -1) Then
            FileWriteLine($Temp, $extract[0])
        EndIf
    EndIf
WEnd
FileClose($file)
Exit

-smartee

Edit: Minor code fixes.

Edited by smartee

Share this post


Link to post
Share on other sites

thank you so much, smartee!

Share this post


Link to post
Share on other sites

Hi,

I came out with the following code. No errors are reported but it is not working right.

It doesn't seem to do the search function. Can someone kindly help me out here....?

Thanks.

#NoTrayIcon
#include <Array.au3>
#include <File.au3>
#include <Process.au3>

Global $YYMMDD = StringMid(@YEAR & @MON & @MDAY, 3)
Global $Temp = @ScriptDir & "\templog" & "_" & $YYMMDD & ".txt"
Global $inifile = @ScriptDir & "\settings.ini"
Global $Search = IniRead($inifile, "config", "Search", "")
Global $Paging = IniRead($inifile, "config", "Paging", "")
Global $LastEOF = IniRead($inifile, "config", "LastEOF", "")
Global $FileLocationName = IniRead($inifile, "config", "FileLocName", "")

Dim $aRecords

If Not FileExists(@ScriptDir & "\settings.ini") Then
    FileInstall("set.ini", @ScriptDir & "\settings.ini", 1)
    MsgBox(0, "Missing configuration file restored", "Please update the default settings.ini before proceeding.")
    Run("notepad.exe " & @ScriptDir & "\settings.ini", "", @SW_MAXIMIZE)
    Exit
EndIf

$Date = IniRead($inifile, "config", "date", "date")
If $Date <> $YYMMDD Then
    IniWrite($inifile, "config", "date", $YYMMDD)
    IniWrite($inifile, "config", "LastEOF", "0")
EndIf

If Not FileExists(@ScriptDir & "\templog" & "_" & $YYMMDD & ".txt") Then
    FileInstall("temp.txt", @ScriptDir & "\templog" & "_" & $YYMMDD & ".txt", 1)
    Exit
EndIf

While 1
    If Not _FileReadToArray($FileLocationName, $aRecords) Then
        MsgBox(4096, "Error", "Error reading log to Array error: " & @error)
        Exit
    EndIf

    MsgBox(0, "Alert", "I am here now.")

    For $x = $LastEOF + 1 To $aRecords[0]

        If ($aRecords[2] = $Search) Then
            $extract = StringRegExp($aRecords[4], "%(?:[^:]+):(.*)", 1)

            If (_ArraySearch($aRecords, $extract[0]) = -1) Then
                FileWriteLine($Temp, $extract[0])
                Run(@ComSpec & " /c " & $Paging, "", @SW_HIDE)
            EndIf
        EndIf

    Next

    $CountLines = _FileCountLines($FileLocationName)
    IniWrite($inifile, "config", "LastEOF", $CountLines)

WEnd
FileClose($file)
Exit

settings.ini.txt

Share this post


Link to post
Share on other sites

oh hi again steelsking :D

I'm not sure exactly what you wanted rectified, so I just sort of patched it up to work like before. :) I hope it works as you like; if not, please describe what exactly you're having trouble with so we can help you better ;)

;#NoTrayIcon
#include <Array.au3>
#include <File.au3>
#include <Process.au3>

Global $YYMMDD = StringMid(@YEAR & @MON & @MDAY, 3)
Global $Temp = @ScriptDir & "\templog" & "_" & $YYMMDD & ".txt"
Global $inifile = @ScriptDir & "\settings.ini"
Global $Search = IniRead($inifile, "config", "Search", "")
Global $Paging = IniRead($inifile, "config", "Paging", "")
Global $LastEOF = IniRead($inifile, "config", "LastEOF", "")
Global $FileLocationName = IniRead($inifile, "config", "FileLocName", "")

Dim $aRecords, $aTemp

If Not FileExists(@ScriptDir & "\settings.ini") Then
    FileInstall("set.ini", @ScriptDir & "\settings.ini", 1)
    MsgBox(0, "Missing configuration file restored", "Please update the default settings.ini before proceeding.")
    Run("notepad.exe " & @ScriptDir & "\settings.ini", "", @SW_MAXIMIZE)
    Exit
EndIf

$Date = IniRead($inifile, "config", "date", "date")
If $Date <> $YYMMDD Then
    IniWrite($inifile, "config", "date", $YYMMDD)
    IniWrite($inifile, "config", "LastEOF", "0")
EndIf

If Not FileExists(@ScriptDir & "\templog" & "_" & $YYMMDD & ".txt") Then
    FileInstall("temp.txt", @ScriptDir & "\templog" & "_" & $YYMMDD & ".txt", 1)
    MsgBox(0, "Info", "TempLog has been FileInstalled. Exiting..")
    Exit
EndIf

While 1
    If Not _FileReadToArray($FileLocationName, $aRecords) Then
        MsgBox(4096, "Error", "Error reading log to Array error: " & @error)
        Exit
    EndIf

    MsgBox(0, "Alert", "I am here (in while loop) now.")

    For $i = $LastEOF + 1 To $aRecords[0] Step 1
        $aParts = StringSplit($aRecords[$i], @TAB)
        If ($aParts[2] = $Search) Then
            $extract = StringRegExp($aParts[4], "%(?:[^:]+):(.*)", 1)
            If Not _FileReadToArray($Temp, $aTemp) Then
                MsgBox(4096, "Error", "Error reading templog to Array error: " & @error)
                Exit
            EndIf
            If (_ArraySearch($aTemp, $extract[0]) = -1) Then
                FileWriteLine($Temp, $extract[0])
                Run(@ComSpec & " /c " & $Paging, "", @SW_HIDE)
            EndIf
        EndIf

    Next

    IniWrite($inifile, "config", "LastEOF", $aRecords[0])
WEnd
Exit

Hope this helps ;)

-smartee

Share this post


Link to post
Share on other sites

hi smartee!

thanks for helping. however I have the following error msg when I run the script.

Line 45

If ($aParts[2] = $Search) Then

If (^ ERROR

Error : Array variable has incorrect number of subscripts or subscript dimension range exceeded.

Appreciate your help again.

SyslogCatchAll.txt

Share this post


Link to post
Share on other sites

what do I want to do exactly?

based on the ini file search, i want the script to go thru the log file and check to see if the search is found. if found and if it is not repeated or found elsewhere, to run a command.

and each time the script runs, it should start from where it last left off as the lines of the log file keep increasing.

thank you!

Share this post


Link to post
Share on other sites

Hi steelsking,

Oh :) luckily you included the test file you used. That error was caused by the couple of lines at the end, you see the script assumed that all data would be lines of tab separated info, however at the end of the file you had this

New Line 1
New Line 2
New Line 3
New Line 4
New Line 5
New Line 6
New Line 7
New Line 8
New Line 9
New Line 100

When the code tried to split these lines by @TAB into an array and find the second element, it failed.

You will never run into this type of problem if the lines above are not part of the expected data, however if it is, a quick couple checks before accessing the array element should do the trick like this: ;)

;#NoTrayIcon
#include <Array.au3>
#include <File.au3>
#include <Process.au3>

Global $YYMMDD = StringMid(@YEAR & @MON & @MDAY, 3)
Global $Temp = @ScriptDir & "\templog" & "_" & $YYMMDD & ".txt"
Global $inifile = @ScriptDir & "\settings.ini"
Global $Search = IniRead($inifile, "config", "Search", "")
Global $Paging = IniRead($inifile, "config", "Paging", "")
Global $LastEOF = IniRead($inifile, "config", "LastEOF", 0)
Global $FileLocationName = IniRead($inifile, "config", "FileLocName", "")

Dim $aRecords, $aTemp

If Not FileExists(@ScriptDir & "\settings.ini") Then
    FileInstall("set.ini", @ScriptDir & "\settings.ini", 1)
    MsgBox(0, "Missing configuration file restored", "Please update the default settings.ini before proceeding.")
    Run("notepad.exe " & @ScriptDir & "\settings.ini", "", @SW_MAXIMIZE)
    Exit
EndIf

$Date = IniRead($inifile, "config", "date", "date")
If $Date <> $YYMMDD Then
    IniWrite($inifile, "config", "date", $YYMMDD)
    IniWrite($inifile, "config", "LastEOF", "0")
EndIf

If Not FileExists(@ScriptDir & "\templog" & "_" & $YYMMDD & ".txt") Then
    FileInstall("temp.txt", @ScriptDir & "\templog" & "_" & $YYMMDD & ".txt", 1)
    MsgBox(0, "Info", "TempLog has been FileInstalled. Exiting..")
    Exit
EndIf

While 1
    If Not _FileReadToArray($FileLocationName, $aRecords) Then
        MsgBox(4096, "Error", "Error reading log to Array error: " & @error)
        Exit
    EndIf
    $LastEOF = IniRead($inifile, "config", "LastEOF", 0)

    TraySetToolTip("I am in while loop now. Lines processed so far: " & $LastEOF) ;Mouse over the icon in the tray to see this message

    For $i = $LastEOF + 1 To $aRecords[0] Step 1
        $aParts = StringSplit($aRecords[$i], @TAB)
        If IsArray($aParts) And (UBound($aParts) >= 5) And ($aParts[2] = $Search) Then
            $extract = StringRegExp($aParts[4], "%(?:[^:]+):(.*)", 1)
            If Not _FileReadToArray($Temp, $aTemp) Then
                MsgBox(4096, "Error", "Error reading templog to Array error: " & @error)
                Exit
            EndIf
            If (_ArraySearch($aTemp, $extract[0]) = -1) Then
                FileWriteLine($Temp, $extract[0])
                Run(@ComSpec & " /c " & $Paging, "", @SW_HIDE)
            EndIf
        EndIf
    Next
    IniWrite($inifile, "config", "LastEOF", $aRecords[0])
WEnd
Exit
This code should also do the bit about starting where it left off ;)

Hope this helps,

-smartee

Share this post


Link to post
Share on other sites

smartee, you rock!!!

thank you so much, its working now!!

appreciate your help all these while. :)

and thanks for the detailed explanation too.

it is helping me in comparing where i went wrong.

cheers!

Share this post


Link to post
Share on other sites

Hi Smartee,

appreciate your kind assistance again.

1) The script is not exiting once it has been run. I want it to exit each time it is run and then continue from last EOF when re-run again.

2) Since the script is not exiting, I have a problem when a new day arrives.

thanks!

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