2017-07-20 84 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] 
} 
相關問題