VBscript搜索字符串并在第二列

时间:2016-09-14 14:40:34

标签: vbscript

我是VBscript的新手,正在寻找一个小帮助。

文件夹中有许多日志文件。我想搜索一个字符串并将其id转换为变量。

日志文件包含多行下面的行。

  

8123行:2月01日19:44:40.961 |信息| JEZHckpTehtA-ADSW4T14T5 |   PROD | 10.86.99.108 | 2016年2月1日19:44:29.595 | EVNT = SWIdmst | DQLN = YN |   DQLN = EQUAL_QUAL_QUEUE_GOHEAD_IT

我想搜索QUAL_QUEUE_GOHEAD_IT并将其ID JEZHckpTehtA-ADSW4T14T5加载到变量并对其执行某些操作。

任何帮助都被大大接受。

1 个答案:

答案 0 :(得分:1)

您可以使用正则表达式。有关我开发的以下内容,请参阅http://regexr.com/3e7qr

 INFO \| ([^\|]*) \|.*QUAL_QUEUE_GOHEAD_IT

在vb中使用它

    testInput = "Line 8123: Feb 01 19:44:40.961 | INFO | JEZHckpTehtA-ADSW4T14T5 | PROD | 10.86.99.108 | Feb 01 2016 19:44:29.595 | EVNT=SWIdmst|DQLN=YN| DQLN=EQUAL_QUAL_QUEUE_GOHEAD_IT" & vbcrlf & _
    "Line 8123: Feb 01 19:44:40.961 | INFO | JEZHckpTehtA-ADSxxxT5 | PROD | 10.86.99.108 | Feb 01 2016 19:44:29.595 | EVNT=SWIdmst|DQLN=YN| DQLN=EQUAL_QUAL_QUEUE_GOHEAD_IT"

    Set re = New RegExp
    re.Pattern = "INFO \| ([^\|]*) \|.*QUAL_QUEUE_GOHEAD_IT"
    re.Global = True
    Set matches = re.Execute(testInput)
    If matches.Count > 0 Then
        msg = "Found " & matches.Count & " matches:" & vbCRLF
        For Each match In Matches
            msg = msg & "Found match """ & match.SubMatches(0) & vbcrlf
        Next
        msgbox msg, 0, "VBScript Regular Expression Tester"
    Else
        msgbox "No match", 0, "VBScript Regular Expression Tester"
    End Ifs

---用文件读取更新----

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\temp\batches"
Set objFolder = objFSO.GetFolder(objStartFolder)

For Each objFile in objFolder.Files

        set oStream = objFile.OpenAsTextStream(1)
        If not oStream.AtEndOfStream Then
                contents = oStream.ReadAll
        End If
        oStream.Close

        Set re = New RegExp
        re.Pattern = "INFO \| ([^\|]*) \|.*QUAL_QUEUE_GOHEAD_IT"
        re.Global = True
        Set matches = re.Execute(contents)

        For Each match In Matches
                varid = match.SubMatches(0)
                ProcessMatch objFile.Path, varid
        Next

Next

sub ProcessMatch(path, id)
    Msgbox "Match " & id & " found in "  & path
end sub