2016-06-09 56 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