我正在尝试制作这个剧本并且它已经彻底失败了。
我想从输入框中收集一个字符串,在文本文件中搜索该字符串1.如果它存在于文本文件1中,则取结果并在文件2中搜索它。结果显示在msgbox或文本文件或我可以复制它的地方。
文件名和位置可以固定在特定位置,无需从输入框输入。
好的,我会再做一个例子来清除它。
Log1.txt
\\10.128.214.01 RU00001 Windows 2002 Serv 0 00:02:54
\\10.128.214.02 RU00002 Windows 2002 Serv 0 00:02:54
\\10.128.214.03 RU00003 Windows 2002 Serv 0 00:02:54
log2.txt
10.128.214.01 RUDH99991 255.255.255.0 10-60-4b-7c-e3-F1 17.07.2013 14:10:21 DHCP
10.128.214.02 RUDH99992 255.255.255.0 10-60-4b-7c-e3-F2 17.07.2013 16:23:40 DHCP
10.128.214.03 RUDH99993 255.255.255.0 10-60-4b-7c-e3-F3 17.07.2013 17:19:30 DHCP
如果我从输入框中搜索字符串RU00001
,我希望看到结果RUDH99991
或其整行。
示例与实际log1
和log2
完全相似,因此上面显示的是log1
和log2
。
这里是来自ACTUAL LOG的2行,每个日志包含大约500行这种格式:
Log1.txt
\\10.135.0.106 RUX0031 Windows 2002 Serv 1 00:01:44
Log2.txt
10.135.0.106 RU-NMR-D0125.dc1.dc2.dc3.net 255.255.255.0 00-FF-FF-FF-19-dd INACTIVE DHCP
数据随着范围等而变化。这是一个大文件,你明白了。
答案 0 :(得分:2)
检查出来:
@echo OFF &SETLOCAL
SET /p "search=Enter search string (ru1, ru2, ru3): "
SET "chain="
SET "found="
FOR /f "delims=() " %%a IN ('^<log1.txt find "%search%"') DO SET "chain=%%a"
IF NOT DEFINED chain ECHO NOT found: %search% & GOTO :EOF
FOR /f "tokens=2delims=() " %%a IN ('^<log2.txt find "%chain%"') DO SET "found=%%a"
IF NOT DEFINED found ECHO NOT found: %chain% & GOTO :EOF
ECHO FOUND %found%
答案 1 :(得分:1)
使用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),则读取文件的整个内容可能会因内存耗尽而导致性能下降。在这种情况下,最好逐行处理文件:
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
添加到本地Intranet区域才能使其正常工作(必须启用安全设置Allow Programmatic clipboard access
)。
答案 2 :(得分:1)
此脚本都将值显示到控制台窗口并将值复制到剪贴板。如果未找到该值,则不显示任何内容,并清除剪贴板。
@echo off
set /p "search=Enter a search term: "
REM Clear the clipboard
(call )|clip
for /f "delims=\ " %%A in (
'findstr /rc:"^[^ ]* *%search% " log1.txt'
) do for /f "tokens=2 delims= " %%B in (
'findstr /rc:"^%%A " log2.txt'
) do (
REM display the value to the screen
echo %%B
REM copy the value (without new line) to the clip board
<nul set /p "=%%B"|clip
)
如果任一文件的格式发生变化,那么很明显FINDSTR搜索字符串以及FOR / F选项可能需要更改。