我知道有很多关于这个话题的问题很难找到,但不幸的是,在我的案例中没有任何帮助过我: 我有一个server.log文件,如下所示:
################################################## ServerLog 07.07.2017 1:00:02,02 Software Version 2.5 (modified 30.06.2017 15:53) ################################################## Number of clients: 4 KB-Server is online --------------------------------------------------------------------- --------------------------------------------------------------------- Client 1 current client: \\192.168.0.22\Dauerversuch01 Connecting the network share successfull? client connected successfully No files found --------------------------------------------------------------------- --------------------------------------------------------------------- Client 2 current client: \\192.168.0.23\Dauerversuch01 Connecting the network share successfull? client connected successfully 3 Files found in the directory Copy from Client to local HDD: "\\192.168.0.23\Dauerversuch01 --> D:\Transfer" All files passed the md5 check Files won't get analysed by GlyphWorks copy files from hdd to server: "D:\Transfer --> \\mucs0244\Pool\CoC-AS\MessdatenQuantum\Dauerversuch_01_DUMMY\2017\07" All files passed the md5 check files were transfered successfully from the client to the server --------------------------------------------------------------------- --------------------------------------------------------------------- Client 3 current client: \\192.168.0.24\Dauerversuch Connecting the network share successfull? client connected successfully 3 Files found in the directory Copy from Client to local HDD: "\\192.168.0.24\Dauerversuch --> D:\Transfer" All files passed the md5 check Files won't get analysed by GlyphWorks copy files from hdd to server: "D:\Transfer --> \\mucs0244\Pool\CoC-AS\MessdatenQuantum\Dauerversuchspruefstand_02_SL20-4\2017\07" All files passed the md5 check files were transfered successfully from the client to the server --------------------------------------------------------------------- --------------------------------------------------------------------- Client 4 current client: \\192.168.0.25\Dauerversuch01 Connecting the network share successfull? client connected successfully 3 Files found in the directory Copy from Client to local HDD: "\\192.168.0.25\Dauerversuch01 --> D:\Transfer" All files passed the md5 check Files won't get analysed by GlyphWorks copy files from hdd to server: "D:\Transfer --> \\mucs0244\Pool\CoC-AS\MessdatenQuantum\Dauerversuch_01_DUMMY2\2017\07" All files passed the md5 check files were transfered successfully from the client to the server --------------------------------------------------------------------- --------------------------------------------------------------------- Batch erfolgreich beendet
您可能已经猜到了,我想将server.log拆分为客户端日志。具体来说,我希望能够以客户端编号作为输入参数运行我的PowerShell脚本,然后输出脚本,例如。 client2.log看起来像这样:
--------------------------------------------------------------------- Client 2 current client: \\192.168.0.23\Dauerversuch01 Connecting the network share successfull? client connected successfully 3 Files found in the directory Copy from Client to local HDD: "\\192.168.0.23\Dauerversuch01 --> D:\Transfer" All files passed the md5 check Files won't get analysed by GlyphWorks copy files from hdd to server: "D:\Transfer --> \\mucs0244\Pool\CoC-AS\MessdatenQuantum\Dauerversuch_01_DUMMY\2017\07" All files passed the md5 check files were transfered successfully from the client to the server ---------------------------------------------------------------------
我能做的最好的就是这个小剧本
$file = (GC H:\server.log)
foreach ($line in $file) {
if ($line -match "^Client \w+") {
$newfile = "$($line.Split(' ')[1]).txt"
} else {
$line | Out-File -Append $newfile
}
}
但是这不能正常工作并且搜索“----”行不起作用。
答案 0 :(得分:2)
使用Select-String
将服务器日志中的客户端部分提取到各个客户端日志文件中:
$serverlog = 'H:\server.log'
$re = '(?ms)----+\r?\n(Client \d+)[\s\S]*?----+'
Select-String -Path $serverlog -Pattern $re -AllMatches |
Select-Object -Expand Matches |
ForEach-Object {
$clientlog = 'C:\path\to\{0}.log' -f $_.Groups[1].Value
$_.Groups[0].Value | Set-Content $clientlog
}
答案 1 :(得分:0)
提供客户编号为1且连续,
此脚本假定日志位于当前文件夹中:
$Splitter='-'*69
$ClientLogs =(Get-Content .\server.log -raw) -split "$Splitter`r?`n$Splitter"
For ($i=1; $i -lt $ClientLogs.Count-1; $i++){
Set-Content -Path (".\Client_$i.log") -Value $ClientLogs[$i]
}