vbscript の使用:
Set fso = CreateObject("Scripting.FileSystemObject")
l1 = fso.OpenTextFile("C:\path\to\log1.txt").ReadAll
l2 = fso.OpenTextFile("C:\path\to\log2.txt").ReadAll
Set re = New RegExp
re.MultiLine = True
're.IgnoreCase = True 'uncomment if you want case-insensitive matches
searchString = InputBox("Enter search string.")
re.Pattern = "\\\\(\S+)\s*" & searchString
For Each m1 In re.Execute(l1)
re.Pattern = "^" & m1.SubMatches(0) & "\s*(\S+)"
For Each m2 In re.Execute(l2)
WScript.Echo m2.SubMatches(0)
Next
Next
でスクリプトを実行するcscript.exe
と、コマンド プロンプトから出力をコピーできます。
入力ファイルが非常に大きい (たとえば、サイズが 1 GB を超える) 場合、ファイルのコンテンツ全体を読み取ると、メモリの枯渇によりパフォーマンスが低下する可能性があります。その場合、ファイルを 1 行ずつ処理する方がよいでしょう。
Set fso = CreateObject("Scripting.FileSystemObject")
Set re = New RegExp
're.IgnoreCase = True 'uncomment if you want case-insensitive matches
searchString = InputBox("Enter search string.")
re.Pattern = "\\\\(\S+)\s*" & searchString
Set f = fso.OpenTextFile("C:\path\to\log1.txt")
Do Until f.AtEndOfStream
For Each m In re.Execute(f.ReadLine)
match = m.SubMatches(0)
Exit Do
Next
Loop
f.Close
If IsEmpty(match) Then WScript.Quit 'no match found
re.Pattern = "^" & match & "\s*(\S+)"
Set f = fso.OpenTextFile("C:\path\to\log2.txt")
Do Until f.AtEndOfStream
For Each m In re.Execute(f.ReadLine)
WScript.Echo m.SubMatches(0)
Exit Do
Next
Loop
f.Close
これは、ファイル処理を関数にカプセル化することで簡素化できます。
Set fso = CreateObject("Scripting.FileSystemObject")
Function FindMatch(filename, pattern)
Set re = New RegExp
re.Pattern = pattern
're.IgnoreCase = True 'uncomment if you want case-insensitive matches
Set f = fso.OpenTextFile(filename)
Do Until f.AtEndOfStream
For Each m In re.Execute(f.ReadLine)
FindMatch = m.SubMatches(0)
Exit Do
Next
Loop
f.Close
End Function
searchString = InputBox("Enter search string.")
match1 = FindMatch("C:\path\to\log1.txt", "\\\\(\S+)\s*" & searchString)
If IsEmpty(match1) Then WScript.Quit 'no match found
match2 = FindMatch("C:\path\to\log2.txt", "^" & match1 & "\s*(\S+)")
If Not IsEmpty(match2) Then WScript.Echo match2
ただし、わずか 500 行のファイルの場合は、コードがはるかに単純であるため、最初のバージョンに固執します。
ところで、見つかった一致をとにかくクリップボードにコピーしたい場合は、次のようにスクリプトから直接行うことができます。
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate("about:blank")
While ie.Busy : WScript.Sleep 100 : Wend
ie.document.ParentWindow.ClipboardData.SetData "text", m.SubMatches(0)
ie.Quit
about:blank
ただし、これを機能させるには、ローカル イントラネット ゾーンに追加する必要があります (セキュリティ設定Allow Programmatic clipboard access
を有効にする必要があります)。