2016-06-09 63 views
0

我有一个独特的ID放在日志文件中,我可以搜索文件并获得它,一旦我找到需要的文件中的唯一ID在这个唯一的ID后面找到另一个字符串(命名为字符串2)并复制字符串2的下一行。在日志文件中找到一个字符串,并在第一个字符串后搜索另一个字符串

请在下面找到我的功能,并请建议如何做到这一点。

Func getAuthResponse($LogfilePath, $AuthRespFilePath, $UniqueId, $search) 

Global $iLine = 0, $sLine = '' 
Global $hFile = FileOpen($LogfilePath) 

If $hFile = -1 Then 
MsgBox(0,'ERROR','Unable to open file for reading.') 
Exit 1 
EndIf ;If $hFile = -1 Then 

; find the line that has the search string 
While 1 
$iLine += 1 
$sLine = FileReadLine($hFile) 

If @error = -1 Then ExitLoop 
    ; finding the unique id in the log file 

    ;ConsoleWrite($UniqueId & @LF) 
    If StringInStr($sLine, $UniqueId) Then 
    ConsoleWrite($sLine & @LF) 
    ; assuming that unique id is found , now finding the phrase Auth response is as follow : after the unique id 
    $sNewLine = $sLine+ 
    If StringInStr($sLine, $search) Then 
     ConsoleWrite($sLine & @LF) 

     //// SOME LOGIC //// 

    ExitLoop 
    EndIf  ;If StringInStr($sLine, $search) Then 

    ExitLoop 
    EndIf  ;If(StringInStr($sLine, $UniqueId) Then 

WEnd  ;While 1 
FileClose($hFile) 
EndFunc 

回答

0

让我们看看,如果我了解,正确:

你需要找到一个ID,afther这个ID的字符串之后,你需要复制下一行。如果这是正确的,我给你一个新的While循环,现在它只是一个For循环。

#include <File.au3> 
For $c1 = 1 To _FileCountLines($hFile) Step +1 
    $sLine = FileReadLine($hFile, $c1) 
    If (StringInStr($sLine, $UniqueId) > 0) Then 
     For $c2 = $c1 To _FileCountLines($hFile) Step +1 
      $sLine = FileReadLine($hFile, $c2) 
      If (StringInStr($sLine, $search) > 0) Then 
       $LINE_AFTER_STRING_2 = FileReadLine($hFile, $c2 + 1) 
       ExitLoop 
      EndIf 
     Next 
    EndIf 
Next 

If $LINE_AFTER_STRING_2 = "" Then MsgBox(0, "NOT FOUND", "NOT FOUND") 

下面的事情发生:首先,它遍历您的ID的所有线条和搜索,如果发现它时,它开始一个新的for循环,并搜索ID后你的字符串,如果发现一个它计数+1行并读取它。这应该是你正在寻找的路线。该变量被称为$LINE_AFTER_STRING_2,随时可以改变这一点。

不要忘了包括File.au3因为我用_FileCountLines

+0

它不适合我的工作。 –

+0

这不是很有帮助。什么不行?哪些代码会出错。这段代码没有经过测试,我只是把它做得很快。 – IkeRoyle

0

试试这个:

#include <File.au3> 

Func getAuthResponse($LogfilePath, $UniqueId, $search) 

    Local $arrayFile = "" 
    Local $output = "" 

    If Not FileExists($LogfilePath) Then Return SetError(1, 0, -1) 

    _FileReadToArray($LogfilePath, $arrayFile) 

    For $i = 1 To $arrayFile[0] 
     If StringInStr($arrayFile[$i], $UniqueId) Then 
      For $j = $i+1 To $arrayFile[0] 
       If StringInStr($arrayFile[$j], $search) Then 
        $output = $arrayFile[$j+1] 
        ExitLoop 
       EndIf 
      Next 
      ExitLoop 
     EndIf  
    Next 

    Return $output 

EndFunc