Syntax Powershell昂贵的解析

Syntax Powershell昂贵的解析,syntax,powershell,Syntax,Powershell,这是我正在写的一段脚本 Get-Content $tempDir\$todaysLog | Where-Object { $_ -match "" } | ForEach-Object -Process { $fields = [regex]::split($_,'@|\s+') Add-Content -Path $importSource2\$todaysLog -value ($($fields[0]) + "`t" + $($fields[1]) + "`t"

这是我正在写的一段脚本

Get-Content $tempDir\$todaysLog | Where-Object { $_ -match "" } |
    ForEach-Object -Process {
    $fields = [regex]::split($_,'@|\s+')
    Add-Content -Path $importSource2\$todaysLog -value ($($fields[0]) + "`t"  + $($fields[1]) + "`t" + $($fields[2]) + " " + $($fields[3])+ "`t" + "<*sender*@"+($($fields[5])) + "`t" + "<*recipient*@"+($($fields[7])))
    }
Get Content$tempDir\$todaysLog | Where Object{$\匹配“”}|
ForEach对象-进程{
$fields=[regex]::split($|,'@|\s+)
添加内容-路径$importSource2\$todaysLog-value($($fields[0])+“`t”+$($fields[1])+“`t”+$($fields[2])+”+“+$($fields[3])+“`t”+”
$s1=“10.197.71.28 SG 0201009 00:00:00sender@kpmg.com "
$s2=$s1-替换“\t[^@\t']+@”,“t*发送方*@”
$s3=$s2-替换“\
$s1=”10.197.71.28 SG 02012009 00:00:00sender@kpmg.com "
$s2=$s1-替换“\t[^@\t']+@”,“t*发送方*@”
$s3=$s2-替换“\
cat$tempDir\$todaysLog|
%{[regex]::替换($),“[A-Z0-9.\%+-]+(@[A-Z0-9.-]+\.[A-Z]{2,4}\s)”,“*发送方*$1*接收方*$2',“IgnoreCase”)}>$importSource2\$todaysLog
日志条目必须看起来像示例行(尤其是sender@kpmg.com部分)


编辑:我做了一些基准测试(1个mo文件(~15000行样本)):

安迪·沃克的解决方案(使用拆分)->18,44s
Measure命令{
获取内容$tempDir\$todaysLog | Where对象{$\匹配“”}|
ForEach对象-进程{
$fields=[regex]::split($|,'@|\s+)
添加内容-路径$importSource2\$todaysLog-value($($fields[0])+“`t”+$($fields[1])+“`t”+$($fields[2])+”+“+$($fields[3])+“`t”+”6,16s
Measure命令{
cat$tempDir\$todaysLog|
%{[regex]::替换($),“[A-Z0-9.\%+-]+(@[A-Z0-9.-]+\.[A-Z]{2,4}\s)”,“*发送方*$1*接收方*$2',“IgnoreCase”)}>$importSource2\$todaysLog
}
cat$tempDir\$todaysLog|
%{[regex]::替换($),“[A-Z0-9.\%+-]+(@[A-Z0-9.-]+\.[A-Z]{2,4}\s)”,“*发送方*$1*接收方*$2',“IgnoreCase”)}>$importSource2\$todaysLog
日志条目必须看起来像示例行(尤其是sender@kpmg.com部分)


编辑:我做了一些基准测试(1个mo文件(~15000行样本)):

安迪·沃克的解决方案(使用拆分)->18,44s
Measure命令{
获取内容$tempDir\$todaysLog | Where对象{$\匹配“”}|
ForEach对象-进程{
$fields=[regex]::split($|,'@|\s+)
添加内容-路径$importSource2\$todaysLog-value($($fields[0])+“`t”+$($fields[1])+“`t”+$($fields[2])+”+“+$($fields[3])+“`t”+”6,16s
Measure命令{
cat$tempDir\$todaysLog|
%{[regex]::替换($),“[A-Z0-9.\%+-]+(@[A-Z0-9.-]+\.[A-Z]{2,4}\s)”,“*发送方*$1*接收方*$2',“IgnoreCase”)}>$importSource2\$todaysLog
}

您应该避免使用Powershell作为大文件大小日志解析的引擎。我会使用logparser.exe(您有一个空格分隔的条目,可以转换为csv),然后在Powershell中使用导入csv来重新创建Powershell对象。从那里您可以剥离和替换字段(基于每个对象).Powershell是胶水而不是燃料。使用它来解析任何大小的大型日志都不是彻头彻尾的愚蠢,但对您和CPU来说都是昂贵的。尽管Lee Holmes在《示例》一书中有一个优秀的Convert-TextObject.ps1,但您需要某种类型的日志解析引擎来处理繁重的工作。

您应该避免使用Powershell作为大文件大小日志解析的引擎。我将使用logparser.exe(您有一个空格分隔的条目,可以转换为csv),然后在Powershell中使用导入csv来重新创建Powershell对象。从那里您可以剥离和替换字段(基于每个对象).Powershell是胶水而不是燃料。使用它来解析任何大小的大型日志都不是彻头彻尾的愚蠢,但对您和CPU来说都是昂贵的。尽管Lee Holmes有一个优秀的Convert-TextObject.ps1,从他的书示例中可以看出,您需要某种类型的日志解析引擎来处理繁重的工作。

有趣的结果。您指的是什么n由“1 mo”?这是多少行?我很想知道Andy Walker处理的文件大小。要执行时间命令,请使用如下度量命令:度量命令{1..1000}有趣的结果。你说的“1 mo”是什么意思“?这是多少行?我很想知道Andy Walker处理的文件的大小。要执行计时命令,请使用类似这样的度量命令:度量命令{1..1000}”
$s1 = "10.197.71.28 SG  02012009 00:00:00   sender@kpmg.com <['recip@kpmg.com.sg']>"
$s2 = $s1 -replace "\t[^@\t']+@", "`t*sender*@"
$s3 = $s2 -replace "\<\['.+@", "<['*recipient*@"
write-host $s3
cat $tempDir\$todaysLog |
  %{ [regex]::Replace($_, "[A-Z0-9._%+-]+(@[A-Z0-9.-]+\.[A-Z]{2,4}\s<\[')[A-Z0-9._%+-]+(@[A-Z0-9.-]+\.[A-Z]{2,4}'\]>)", '*sender*$1*recipients*$2', "IgnoreCase") } > $importSource2\$todaysLog
Measure-Command {

Get-Content $tempDir\$todaysLog | Where-Object { $_ -match "" } |
    ForEach-Object -Process {
    $fields = [regex]::split($_,'@|\s+')
    Add-Content -Path $importSource2\$todaysLog -value ($($fields[0]) + "`t"  + $($fields[1]) + "`t" + $($fields[2]) + " " + $($fields[3])+ "`t" + "<*sender*@"+($($fields[5])) + "`t" + "<*recipient*@"+($($fields[7])))
    }

}
Measure-Command {

Get-Content $tempDir\$todaysLog | Where-Object { $_ -match "" } |
    ForEach-Object -Process {
    $s2 = $_ -replace "\t[^@\t']+@", "`t*sender*@"
    $s3 = $s2 -replace "\<\['.+@", "<['*recipient*@"
    Add-Content -Path $importSource2\$todaysLog -value $s3
    }

}
Measure-Command {

cat $tempDir\$todaysLog |
  %{ [regex]::Replace($_, "[A-Z0-9._%+-]+(@[A-Z0-9.-]+\.[A-Z]{2,4}\s<\[')[A-Z0-9._%+-]+(@[A-Z0-9.-]+\.[A-Z]{2,4}'\]>)", '*sender*$1*recipients*$2', "IgnoreCase") } > $importSource2\$todaysLog

}