我有一个我想要阅读的文本文件。看起来像这样
错误:死锁
Param 0 = xyx
Param 1 = 22332244
Param 2 =
Param 3 = 1
Param 4 =
我需要搜索String“Deadlock”并为Param 0和Param 1吐出输出。现在我只能读取包含文本死锁的行:(
Const ForReading = 1
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "deadlock"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\1\Retrieve.log", ForReading)
Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set colMatches = objRegEx.Execute(strSearchString)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
Wscript.Echo strSearchString
Next
End If
Loop
objFile.Close
Const ForReading = 1
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "deadlock"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\1\Retrieve.log", ForReading)
Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set colMatches = objRegEx.Execute(strSearchString)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
Wscript.Echo strSearchString
Next
End If
Loop
objFile.Close
答案 0 :(得分:0)
Marc B在简单代码中的想法:
Option Explicit
Function qq(sTxt) : qq = """" & sTxt & """" : End Function
Const csFind = "Error: Deadlock"
Dim nLines : nLines = 3
Dim bFound : bFound = False
Dim tsIn : Set tsIn = CreateObject("Scripting.FileSystemObject").OpenTextFile("..\data\Text-1.txt")
Do Until tsIn.AtEndOfStream
Dim sLine : sLine = tsIn.ReadLine()
If bFound Then
WScript.Echo sLine, "=>", qq(Trim(Split(sLine, "=")(1)))
nLines = nLines - 1
If 0 = nLines Then Exit Do
Else
bFound = sLine = csFind
End If
Loop
tsIn.Close
输出:
type ..\data\Text-1.txt
Error: Deadlock
Param 0 = xyx
Param 1 = 22332244
Param 2 =
Param 3 = 1
Param 4 =
Error: This is no Deadlock
Param 0 = abc
Param 1 = def
Param 2 =
Param 3 = ghi
Param 4 =
DNV35 E:\trials\SoTrials\answers\9812373\vbs
cscript 00.vbs
Param 0 = xyx => "xyx"
Param 1 = 22332244 => "22332244"
Param 2 = => ""
“简单”意味着“正是解决问题所需要的 - 不多也不少”。