2017-07-20 56 views
0

我知道有关于这个话题的许多问题是awnsered alreade,但不幸的是没有帮助我在我的情况: 我有一个server.log文件看起来像这样:分割文本文件使用PowerShell的线和字符串

 
################################################## 
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

也许你应该改变这种软件的记录,你用它来创建这些呢?解析自由格式的文本文件是一个真正的痛苦。 – Vesper

+0

不是真的可能不幸,但我可以改变软件说只包括一个“--------”线,但我不知道如何去做,然后...... –

+0

“(?s) (?= Client \ s \ d)。*?(?= ---)“应该与客户端的完整块匹配。然后,如果我不记得错误,匹配将在数组(?)$ match中循环,并根据匹配中的第一行写出日志。 _(可能是一些更好的写正则表达式的方法)_ – Jonas

回答

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 
    } 
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] 
} 
相关问题