我知道有关于这个话题的许多问题是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
}
}
但是,这并不正确,工作搜索的“----”行不行。
也许你应该改变这种软件的记录,你用它来创建这些呢?解析自由格式的文本文件是一个真正的痛苦。 – Vesper
不是真的可能不幸,但我可以改变软件说只包括一个“--------”线,但我不知道如何去做,然后...... –
“(?s) (?= Client \ s \ d)。*?(?= ---)“应该与客户端的完整块匹配。然后,如果我不记得错误,匹配将在数组(?)$ match中循环,并根据匹配中的第一行写出日志。 _(可能是一些更好的写正则表达式的方法)_ – Jonas