【发布时间】:2022-01-06 05:06:35
【问题描述】:
我有这个 PowerShell 脚本来一次解析许多看起来像配置文件的文本文件(大约 1 MB):
脚本:
$counter = ($false,0,0)
$objcounter = 0
$global:files = [ordered]@{}
$txt = [System.IO.File]::ReadAllLines($opath)
foreach($line in $txt){
if ($counter[2] -eq "spline"){if ($counter[1] -eq 1){$counter[1]++}else{$key=$global:files.Keys;if (-not($global:files.Contains($line))){$global:files+=[ordered]@{$line=@{path=$line;type="spline"}}};$counter = ($false,0,0)}}
elseif ($counter[2] -eq "object"){if ($counter[1] -eq 1){$counter[1]++}else{$key=$global:files.Keys;if (-not($global:files.Contains($line))){$global:files+=[ordered]@{$line=@{path=$line;type="sceneryobject"}}};$counter = ($false,0,0)}}
elseif ($counter[2] -eq "splineh"){if ($counter[1] -eq 1){$counter[1]++}else{$key=$global:files.Keys;if (-not($global:files.Contains($line))){$global:files+=[ordered]@{$line=@{path=$line;type="splineh"}}};$counter = ($false,0,0)}}
elseif ($counter[2] -eq "attachedobject"){if ($counter[1] -eq 1){$counter[1]++}else{$key=$global:files.Keys;if (-not($global:files.Contains($line))){$global:files+=[ordered]@{$line=@{path=$line;type="attachedobject"}}};$counter = ($false,0,0)}}
elseif ($counter[2] -eq "splineattachement"){if ($counter[1] -eq 1){$counter[1]++}else{$key=$global:files.Keys;if (-not($global:files.Contains($line))){$global:files+=[ordered]@{$line=@{path=$line;type="splineattachement"}}};$counter = ($false,0,0)}}
if ($line -eq "[spline]"){
$counter = @($true,1,"spline");$objcounter++}
if ($line -eq "[splineh]"){
$counter = @($true,1,"object");$objcounter++}
if ($line -eq "[object]"){
$counter = @($true,1,"object");$objcounter++}
if ($line -eq "[attachObj]"){
$counter = @($true,1,"attachedobject");$objcounter++}
if ($line -eq "[splineAttachement]"){
$counter = @($true,1,"splineattachement");$objcounter++}
}
(我知道它的结构不好。)
文件:
[spline]
0
apath\path\file3.ext
8947
8946
8992
0.0584106412565594
0.250000081976033
195.973568100565
90.0000020235813
39.99999937227
0
0
0
0
0
0
0
180.853118555128
[spline_h]
0
apath\path\file2.ext
8949
8948
9022
0.0565795901830857
0.250000202235118
202.972286028874
90.0000020235813
39.99999937227
0
0
0
0
0
0
0
183.907441598005
mirror
[spline]
0
apath\path\file.ext
8951
0
9019
0.0585327145350332
0.0999999434550936
201.971026072961
90.0000020235813
39.99999937227
0
0
0
0
0
0
0
183.47110728047
mirror
(等等……)
脚本运行良好,但解析文件需要很长时间,一段时间后我得到“无响应”并且应用程序崩溃。
这是我需要的输出:
$global:files = [ordered]@{path=@{path="path";type="type"}}
其中“路径”是文件路径,例如:apath\path\file.ext 和
'type' 是网格类型,例如:spline 或 spline_h。
我可以改变什么来加快解析速度?
【问题讨论】:
-
您能否添加一个示例,说明您需要的输出是什么?对我来说,这还不清楚..
-
@Theo 当然。所需的输出是 [ordered]。
-
您能否解释一下脚本在做什么以及它的条件是什么,此时我们可能能够以更有效的方式重新创建它,但
if条件似乎过于复杂。我知道这是一个哈希表,其中[..]之间的每个关键字的paths作为键,值是路径和关键字。 -
@Theo 我敢肯定,到目前为止,您将能够改进我的答案,尤其是在
regex部分,尽管 OP 之前需要澄清某些点