有幾個問題已經是這樣,但沒有一個足夠具體用於我的目的。搜索特定字符串的10,000個日誌文件,然後輸出該字符串所在的每行,同時還創建該日誌文件的副本
我需要搜索特定字符串的10,000個日誌文件,然後輸出該字符串所在的每行,同時還創建該日誌文件的副本。
我幾乎在BATCH文件中工作..我想我打我的牆,需要開始使用PowerShell,我以前沒有用太多。
:更新
感謝Trondh,我能夠用自己的腳本作爲一個完美的基地,並把在我需要的功能。希望這可以幫助其他人:)
#Folder to search
$folder = read-host "Please specify the location of the search "
#Search for:
$SearchTerm = read-host "type in the word you want to find Eg. Error or JobID "
#Files to include in search
$FileFilter = read-host "Enter Date Part of file filter Eg. 2014or 201401 "
#File to store log file copies in
$destinationfolder = "Backup-$SearchTerm"
#File to store results in
$newfile = "Search-Log-$SearchTerm.txt"
#Get the files according to filter. Recurse, but exclude directories
$files = Get-ChildItem -Path $folder -Include @("*$filefilter*.*") -recurse | where {$_.PSIsContainer -eq $false}
foreach ($file in $files)
{
$result = $file | Select-String $SearchTerm
$result | add-content $newfile
New-Item -ItemType Directory -Force -Path $destinationfolder
#If we get a hit, copy the file
if ($result)
{
Write-host "Found match in file $($file.Name) ($($file.Directory))"
#Add result to file
$file | Copy-Item -Destination $destinationfolder
#Also output it
$result
}
}
Write-Host "Search Completed!"
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
我的建議是使用以C++,C#,JAVA等爲本機(或幾乎)代碼輸出的語言。因爲批量解析文件需要很長時間 –