【问题标题】:find a string in log file and search another string after the first string在日志文件中查找一个字符串并在第一个字符串之后搜索另一个字符串
【发布时间】:2016-10-09 21:28:15
【问题描述】:

我在日志文件中放置了一个唯一 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 

【问题讨论】:

    标签: string autoit


    【解决方案1】:

    试试这个:

    #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
    

    【讨论】:

      【解决方案2】:

      让我们看看我是否理解正确:

      你需要找到一个 ID,在这个 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

      【讨论】:

      • 那不是很有帮助。什么不起作用?哪个代码出错。此代码未经测试,我只是让它非常快。
      猜你喜欢
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 2014-11-10
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多