【问题标题】:search through 10,000s of log files for a specific string then out put each line that string is in while also creating a copy of that log file在 10,000 个日志文件中搜索特定字符串,然后输出该字符串所在的每一行,同时创建该日志文件的副本
【发布时间】:2023-03-15 02:25:01
【问题描述】:

已经有一些这样的问题,但没有一个对我的目的来说足够具体。

我需要在 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. 20140123 or 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 等。因为批量解析文件需要很长时间

标签: batch-file powershell xcopy findstr


【解决方案1】:

我会这样做:

#Folder to search
$folder = "D:\trond.hindenes\Desktop\test"
#File to store log file copies in
$destinationfolder = "D:\trond.hindenes\Desktop\test2"
#Search for:
$SearchTerm = "BAT"
#Files to include in search
$FileFilter = "file*"

#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

        #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

            }

    }

【讨论】:

  • 它会搜索字符串,但不会复制文件。
  • 抱歉,没有正确阅读问题。查看更新的答案。
  • +1 不错的作品.. 现在我只需要弄清楚用户输入的变量如何在 powershell 中工作不应该很难:P
  • 刚刚注意到您删除了将信息写入文本文件,但我想我可以弄清楚:)
  • 感谢您的代码,一切正常。 :) 我会用代码更新我的主线程。
【解决方案2】:
....
....
rem search for files that contain data
for /f "tokens=*" %%f in ('findstr /s /i /m /c:"%word%" "%drive%\*%data%*.log"') do (

    rem copy the file selected by findstr to target folder
    copy "%%~ff" "%targetFolder%"

    rem and echo the lines with the data to the results file
    findstr /n /i /c:"%word%" "%%~ff" >> "Search-Logs-Results-%word%.TXT"
)

findstr /m 只是测试文件中是否存在字符串,并在第一次匹配时保留文件,将文件名写入标准输出。使用 for 命令处理文件列表。对于每一个,都会复制文件,然后将带有所需单词的行发送到报告文件中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 2016-10-09
    • 2013-02-11
    • 1970-01-01
    • 2017-06-16
    相关资源
    最近更新 更多