2013-07-09 24 views
-4

我試圖讓這個腳本,它已經徹底失敗。搜索字符串1 2文件的步驟

我想收集從一個輸入框的字符串,搜索文本文件1.該字符串如果在文本文件中1存在,則取結果,並尋找它在文件2的結果是秀在msgbox或文本文件或一些我可以複製它的地方。

文件的名稱和位置可以固定在特定位置無需輸入它們形成的輸入框。

OK,我會讓另一個例子來清除它了。

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或它的全系列。

示例與實際的log1log2完全相似,因此上面顯示的是log1log2


這裏是從實際日誌每個日誌包含大約500行這種格式的2線:

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

如果您發現log1.txt RU 1,你怎麼知道你要在log2.txt DH1? – Jonesopolis

+0

你的問題還不太清楚,當你找到你的第一個字符串時,我假設你想要返回它旁邊的int而不是字符串本身,對吧?爲此使用XML。 – Master117

+0

@Jonest他們都有類似的字符串,這是10或20或30 – Ti2

回答

1

使用

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)。

+0

在所有隊友:( – Ti2

+1

嗯,這是有點令人吃驚,當你實際的文件看起來完全不同於任何結果山姆您之前提供的數據。根據修改的要求更新答案。 –

+0

我曾與不斷變化的需求同樣悲痛:/ – dbenham

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% 
+0

伴侶你有它關閉 – Ti2

+0

2東西交配,你可以使它的工作:第一 – Ti2

+0

前(\\ 10.128.214.01 RU00001 Windows 2002服務000:02: 54)在becouse的log1.txt \\它不能找到匹配,以便它具有與\\ – Ti2

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選項可能不得不改變。

+0

對我來說沒有作用bro – Ti2

+0

@ user2566032,我原來的答案「沒有工作」,因爲它是根據你原來不符合你的實際要求的差規格編寫的。繼續不正確地指定你想要的,你可以期待「不工作」的答案。我的答案已經過編輯以符合您最新指定的要求,並且我已驗證它可與您的樣本數據一起使用。 – dbenham

+0

隊友我試了一下,即使與exm數據並沒有顯示結果和剪輯是清楚的 – Ti2