我有多个服务器日志文件。它们总共包含大约500.000行日志文本。我只想保留包含“已下载”和“日志”的行。我想排除的代码集中在错误日志和基本的系统操作上,比如“客户端启动”、“客户端重新启动”等等。
下面是我们要查找的代码行的一个示例:
[22:29:05]: Downloaded 39 /SYSTEM/SAP logs from System-4, customer (000;838) from 21:28:51,705 to 21:29:04,671要保留的行应该由日期字符串补充,日期字符串是日志文件名的一部分。($date)
此外,由于接收到的日志相当无结构,过滤后的文件应转换为一个csv文件(列:时间戳、日志下载、系统目录、系统类型、客户、开始时间、结束时间、日期,以添加到文件名的每一行。将空格转换为逗号的替换操作只是在数据中引入一些结构的第一次尝试。这个文件应该加载到python仪表板程序中。
目前,预处理3个Txt文件需要2,5分钟,而目标最多5-10秒,如果可能的话。
真的非常感谢你的支持,因为我从上周一开始就一直在努力。也许powershell不是最好的选择?我愿意接受任何帮助!
目前,我正在运行这个powershell脚本:
$files = Get-ChildItem "C:\Users\AnonUser\RestLogs\*" -Include *.log
New-Item C:\Users\AnonUser\RestLogs\CleanedLogs.txt -ItemType file
foreach ($f in $files){
$date = $f.BaseName.Substring(22,8)
(Get-Content $f) | Where-Object { ($_ -match 'Downloaded' -and $_ -match 'SAP')} | ForEach-Object {$_ -replace " ", ","}{$_+ ','+ $date} | Add-Content CleanedLogs.txt
}发布于 2019-03-25 18:58:25
这差不多是我能做到的最快的了。我没有使用-split与-replace或特殊的.NET方法进行测试:
$files = Get-ChildItem "C:\Users\AnonUser\RestLogs\*" -Include *.log
New-Item C:\Users\AnonUser\RestLogs\CleanedLogs.txt -ItemType file
foreach ($f in $files) {
$date = $f.BaseName.Substring(22,8)
(((Get-Content $f) -match "Downloaded.*?SAP") -replace " ",",") -replace "$","$date" | add-content CleanedLogs.txt
}一般来说,速度是通过删除循环和Where-Object“过滤”来获得的。
https://stackoverflow.com/questions/55334848
复制相似问题