您好我是PowerShell的新手并且正在探索它。
要求:我有以下结构的多个:
SAGENT EXPORT FILE
VERSION 18
LOCALE_INFO "1252",".","","-",".".
BEGIN PLAN
BEGIN ATTRIBUTES
name=Untitled1
last_saved_date=2456999,08:38:27.000
i18n=12
END ATTRIBUTES
BEGIN STEP 1
BEGIN ATTRIBUTES
name=Subplan
step_id={40A4A455-4859-4229-BF2D-FB350941CBF7}
step_type=11
is_component=0
END ATTRIBUTES
BEGIN PROPERTY 1
BEGIN ATTRIBUTES
name=AllProperties
value_type=1
custom=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=143D7401495BDB81982EFA7BF09092DD6449C6FA21230D16E61D8B7DE1850145531D767D97E2309CD3D2136346472177948123402583722181E3E5732567B4BE348173CA4E3A61F04D69085546C 65-1DEE05E0-BA48-11CF-B2C7-00A02448857E
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 1
BEGIN STEP 2
BEGIN ATTRIBUTES
name=Time Generation
step_id={77EDB022-4507-488A-95C0-91C8E22F1F6B}
display_x=156
display_y=84
END ATTRIBUTES
BEGIN STEPTYPE
BEGIN ATTRIBUTES
name=Time Generation
clsid={4B343B8B-BCED-11D0-B0B7-00A0C91FDBFC}
step_type=5
input_count=1
output_count=1
sink=0
source=1
dc_step_type=303
icon_id=0
visibility=ISD/T
rt_clsid={fe294e69-d35a-11ce-814a-0020afea3965}
ct_clsid={fe294e69-d35a-11ce-814a-0020afea3965}
ui_clsid={fe294e69-d35a-11ce-814a-0020afea3965}
tip_text=Generates date and time values in the Data Flow
END ATTRIBUTES
END STEPTYPE
BEGIN PROPERTY 1
BEGIN ATTRIBUTES
name=julian_day
value_type=3
val_int=0
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 1
BEGIN PROPERTY 2
BEGIN ATTRIBUTES
name=input_type
value_type=3
val_int=0
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 2
BEGIN PROPERTY 3
BEGIN ATTRIBUTES
name=duration
value_type=3
val_int=1
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 3
BEGIN PROPERTY 4
BEGIN ATTRIBUTES
name=num_cols
value_type=3
val_int=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 4
BEGIN PROPERTY 5
BEGIN ATTRIBUTES
name=type_0
value_type=3
val_int=4
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 5
BEGIN PROPERTY 6
BEGIN ATTRIBUTES
name=col_0
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=Year
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 6
BEGIN PROPERTY 7
BEGIN ATTRIBUTES
name=portguid_0
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]={4A5DD351-04A6-4EDB-87C1-071B97771144}
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 7
END STEP 2
BEGIN STEP 3
BEGIN ATTRIBUTES
name=Expression Calculator
step_id={691F69C4-5FF6-4DF5-A9C9-FB24EA418AB6}
display_x=294
display_y=96
END ATTRIBUTES
BEGIN STEPTYPE
BEGIN ATTRIBUTES
name=Expression Calculator
clsid={4B343BB3-BCED-11D0-B0B7-00A0C91FDBFC}
step_type=5
input_count=1
output_count=1
sink=0
source=0
dc_step_type=303
icon_id=0
visibility=IS/G
rt_clsid={fe294e37-d35a-11ce-814a-0020afea3965}
ct_clsid={fe294e37-d35a-11ce-814a-0020afea3965}
ui_clsid={fe294e37-d35a-11ce-814a-0020afea3965}
tip_text=Uses expressions to add calculated columns
END ATTRIBUTES
END STEPTYPE
BEGIN PROPERTY 1
BEGIN ATTRIBUTES
name=ErrorCode
value_type=3
val_int=0
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 1
BEGIN PROPERTY 2
BEGIN ATTRIBUTES
name=USERINIT
value_type=3
val_int=1
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 2
BEGIN PROPERTY 3
BEGIN ATTRIBUTES
name=FieldOrders
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=-1;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 3
BEGIN PROPERTY 4
BEGIN ATTRIBUTES
name=ExpressionText0
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=ToString (Year )
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 4
BEGIN PROPERTY 5
BEGIN ATTRIBUTES
name=ExprScales
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=0;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 5
BEGIN PROPERTY 6
BEGIN ATTRIBUTES
name=FieldPassThroughs
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=0;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 6
BEGIN PROPERTY 7
BEGIN ATTRIBUTES
name=ExprIds
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=1;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 7
BEGIN PROPERTY 8
BEGIN ATTRIBUTES
name=ExprEnableds
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=1;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 8
BEGIN PROPERTY 9
BEGIN ATTRIBUTES
name=UpdateInPlace
value_type=3
val_int=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 9
BEGIN PROPERTY 10
BEGIN ATTRIBUTES
name=ExprNames
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=strYear;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 10
BEGIN PROPERTY 11
BEGIN ATTRIBUTES
name=FieldNames
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=Year;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 11
BEGIN PROPERTY 12
BEGIN ATTRIBUTES
name=PropAltNames
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 12
BEGIN PROPERTY 13
BEGIN ATTRIBUTES
name=IncludeNewByDefault
value_type=3
val_int=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 13
BEGIN PROPERTY 14
BEGIN ATTRIBUTES
name=ExprLengths
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=0;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 14
BEGIN PROPERTY 15
BEGIN ATTRIBUTES
name=Version
value_type=3
val_int=8
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 15
BEGIN PROPERTY 16
BEGIN ATTRIBUTES
name=FieldAltNames
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=Year;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 16
BEGIN PROPERTY 17
BEGIN ATTRIBUTES
name=FieldLengths
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=4;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 17
BEGIN PROPERTY 18
BEGIN ATTRIBUTES
name=ExprCreateNews
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=1;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 18
BEGIN PROPERTY 19
BEGIN ATTRIBUTES
name=ExprOrders
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=0;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 19
BEGIN PROPERTY 20
BEGIN ATTRIBUTES
name=FieldIds
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=##FIELDID##{77EDB022-4507-488A-95C0-91C8E22F1F6B}0;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 20
BEGIN PROPERTY 21
BEGIN ATTRIBUTES
name=Copy
value_type=3
val_int=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 21
BEGIN PROPERTY 22
BEGIN ATTRIBUTES
name=FieldDataTypes
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=2;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 22
BEGIN PROPERTY 23
BEGIN ATTRIBUTES
name=ExprExstColumn
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 23
BEGIN PROPERTY 24
BEGIN ATTRIBUTES
name=PropNames
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 24
BEGIN PROPERTY 25
BEGIN ATTRIBUTES
name=ExpressionNextIDIndex
value_type=3
val_int=2
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 25
BEGIN PROPERTY 26
BEGIN ATTRIBUTES
name=ExprPrecisions
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=0;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 26
BEGIN PROPERTY 27
BEGIN ATTRIBUTES
name=ExprAutoLengths
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=1;
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 27
END STEP 3
BEGIN STEP 4
BEGIN ATTRIBUTES
name=Rename Columns
step_id={DA66A76B-3F95-4257-95AE-6BE49957164F}
display_x=450
display_y=96
END ATTRIBUTES
BEGIN STEPTYPE
BEGIN ATTRIBUTES
name=Rename Columns
clsid={4B343BAA-BCED-11D0-B0B7-00A0C91FDBFC}
step_type=5
input_count=1
output_count=1
sink=0
source=0
dc_step_type=303
icon_id=0
visibility=IS/G
rt_clsid={fe294e20-d35a-11ce-814a-0020afea3965}
ct_clsid={fe294e20-d35a-11ce-814a-0020afea3965}
ui_clsid={fe294e20-d35a-11ce-814a-0020afea3965}
tip_text=Changes column names
END ATTRIBUTES
END STEPTYPE
BEGIN PROPERTY 1
BEGIN ATTRIBUTES
name=USERINIT
value_type=3
val_int=1
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 1
BEGIN PROPERTY 2
BEGIN ATTRIBUTES
name=UpdateInPlace
value_type=3
val_int=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 2
BEGIN PROPERTY 3
BEGIN ATTRIBUTES
name=Version
value_type=3
val_int=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 3
BEGIN PROPERTY 4
BEGIN ATTRIBUTES
name=FieldCount
value_type=3
val_int=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 4
BEGIN PROPERTY 5
BEGIN ATTRIBUTES
name=Copy
value_type=3
val_int=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 5
END STEP 4
BEGIN STEP 5
BEGIN ATTRIBUTES
name=Filter
step_id={D98A52A5-B54D-42E9-8ECC-A905A4135537}
display_x=576
display_y=108
END ATTRIBUTES
BEGIN STEPTYPE
BEGIN ATTRIBUTES
name=Filter
clsid={4B343BA1-BCED-11D0-B0B7-00A0C91FDBFC}
step_type=5
input_count=1
output_count=1
sink=0
source=0
dc_step_type=303
icon_id=0
visibility=IS/G
rt_clsid={fe294e0d-d35a-11ce-814a-0020afea3965}
ct_clsid={fe294e0d-d35a-11ce-814a-0020afea3965}
ui_clsid={fe294e0d-d35a-11ce-814a-0020afea3965}
tip_text=Removes rows using filter conditions
END ATTRIBUTES
END STEPTYPE
BEGIN PROPERTY 1
BEGIN ATTRIBUTES
name=MatchCase
value_type=3
val_int=1
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 1
BEGIN PROPERTY 2
BEGIN ATTRIBUTES
name=TEXTVALUE
value_type=1
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=Abc
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 2
BEGIN PROPERTY 3
BEGIN ATTRIBUTES
name=strOperator
value_type=1
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]==
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 3
BEGIN PROPERTY 4
BEGIN ATTRIBUTES
name=FIELD
value_type=1
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=strYear
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 4
BEGIN PROPERTY 5
BEGIN ATTRIBUTES
name=strValueType
value_type=1
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=text
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 5
BEGIN PROPERTY 6
BEGIN ATTRIBUTES
name=USERINIT
value_type=3
val_int=1
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 6
BEGIN PROPERTY 7
BEGIN ATTRIBUTES
name=EXPRESSIONS
value_type=3
val_int=0
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 7
BEGIN PROPERTY 8
BEGIN ATTRIBUTES
name=FIELDID
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=##FIELDID##{691F69C4-5FF6-4DF5-A9C9-FB24EA418AB6}1
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 8
BEGIN PROPERTY 9
BEGIN ATTRIBUTES
name=UpdateInPlace
value_type=3
val_int=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 9
BEGIN PROPERTY 10
BEGIN ATTRIBUTES
name=VALUETYPE
value_type=3
val_int=11
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 10
BEGIN PROPERTY 11
BEGIN ATTRIBUTES
name=Version
value_type=3
val_int=3
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 11
BEGIN PROPERTY 12
BEGIN ATTRIBUTES
name=Copy
value_type=3
val_int=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 12
BEGIN PROPERTY 13
BEGIN ATTRIBUTES
name=FIELDIDVALUE
value_type=1
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=##FIELDID##{D98A52A5-B54D-42E9-8ECC-A905A4135537}-1
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 13
BEGIN PROPERTY 14
BEGIN ATTRIBUTES
name=OPERATOR
value_type=3
val_int=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
END ATTRIBUTES
END PROPERTY 14
END STEP 5
BEGIN STEP 6
BEGIN ATTRIBUTES
name=Grid
step_id={63A4BCDA-3C2D-424E-B14D-32BFB67BDFF3}
display_x=720
display_y=126
END ATTRIBUTES
BEGIN STEPTYPE
BEGIN ATTRIBUTES
name=Grid
clsid={4B343B81-BCED-11D0-B0B7-00A0C91FDBFC}
step_type=2
input_count=1
output_count=1
sink=1
source=0
dc_step_type=304
icon_id=0
visibility=IS/C
ui_clsid={fe294e93-d35a-11ce-814a-0020afea3965}
tip_text=Displays results in column format
END ATTRIBUTES
END STEPTYPE
BEGIN PROPERTY 1
BEGIN ATTRIBUTES
name=PresentationColumnInformation
value_type=1
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=0 3 BEGIN_COL 113 0 ##FIELDID##{691F69C4-5FF6-4DF5-A9C9-FB24EA418AB6}1 strYear 3 262144 0 0 0 0 0 END_COL
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 1
BEGIN PROPERTY 2
BEGIN ATTRIBUTES
name=PresentationParameterInformation
value_type=1
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=0 3 0 1 0 4000 1 0 1400
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 2
BEGIN PROPERTY 3
BEGIN ATTRIBUTES
name=PresentationColumnHeadingInformation
value_type=1
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=0 3 0 404 16498431 1 4 0 52520 0 0 0 0 0 0 0 0 0 2 16777215 Arial,8,700,0,0,0
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 3
BEGIN PROPERTY 4
BEGIN ATTRIBUTES
name=PresentationRowHeadingInformation
value_type=1
custom=0
is_list=0
value_attrs=0
openlink=0
is_proxy=0
BEGIN VECTOR val_string val_string_pos
[1]=0 3 0 404 16498431 0 0 0 52520 0 0 255 255 255 0 0 0 0 0 -167772160 Arial,8,700,0,0,0
[1]=0
END VECTOR
END ATTRIBUTES
END PROPERTY 4
END STEP 6
END STEP 1
BEGIN FLOW 1
BEGIN ATTRIBUTES
name=Flow
source_output_no=0
dest_input_no=0
END ATTRIBUTES
SOURCE_STEP=6210134672605708290
DESTINATION_STEP=6210134672605708291
END FLOW 6210134672605708289
BEGIN FLOW 2
BEGIN ATTRIBUTES
name=Flow
source_output_no=0
dest_input_no=0
END ATTRIBUTES
SOURCE_STEP=6210134672605708291
DESTINATION_STEP=6210134672605708292
END FLOW 6210134672605708290
BEGIN FLOW 3
BEGIN ATTRIBUTES
name=Flow
source_output_no=0
dest_input_no=0
END ATTRIBUTES
SOURCE_STEP=6210134672605708292
DESTINATION_STEP=6210134672605708293
END FLOW 6210134672605708291
BEGIN FLOW 4
BEGIN ATTRIBUTES
name=Flow
source_output_no=0
dest_input_no=0
END ATTRIBUTES
SOURCE_STEP=6210134672605708293
DESTINATION_STEP=6210134672605708294
END FLOW 6210134672605708292
END PLAN
END
现在我需要扫描所有文件并检查过滤器部分中缺少哪个部分,因为会有两种情况,即下面部分将可用或将丢失
BEGIN VECTOR val_string val_string_pos
[1]=Abc
[1]=0
END VECTOR
以上部分将始终位于“过滤步骤”下,但步骤编号可能会有所不同
以下是我的计划:
如果成功找到模式
BEGIN STEP 5
BEGIN ATTRIBUTE
name = filter
(注意数字5因不同的文件而异,我想保存在临时变量中说x)
搜索END step(x)
并保存在y
现在搜索
BEGIN VECTOR val_string val_string_pos
[1]=Abc
[1]=0
END VECTOR
在上限和下限之间x和y
如果找到,请将变量中的文件名保存到列表中以获取所有此类计划,其中包含哪些计划
我不确定接下来的方法。任何指导将不胜感激
答案 0 :(得分:4)
在为这些格式编写解析代码时,我发现在许多情况下,使用switch -Regex
是一种很好的方法。您在解析期间将状态保存在各种变量中,并根据行匹配的正则表达式输入不同的大小写。
这样的事情:
filter Add-Property ($name, $value) {
$_ | Add-Member NoteProperty $name $value
}
switch -regex ($fileName) {
'^SAGENT EXPORT FILE$' {
# file start
$file = New-Object PSObject
}
'^VERSION (\d+)$' {
$file | Add-Property Version ([int]$Matches[1])
}
# ...
'^BEGIN PLAN$' {
$plan = New-Object PSObject
$context = $plan
}
'^\s+BEGIN ATTRIBUTES$' {
$attr = New-Object PSObject
}
'name=(.*)' {
$attr | Add-Property Name ($Matches[1])
}
# ...
'^\s+END ATTRIBUTES$' {
$context | Add-Property Attributes $attr
}
# ...
'^END PLAN$' {
$file | Add-Property Plan $plan
}
}
如果有了这些,您可以在结构中查找搜索内容。 PowerShell的管道在处理对象(我们在上面构建)时非常容易。 E.g。
$matchingFiles = $files |
where {
$_.Plan.Steps | % { $_.Properties } | % { $_.Attributes } |
where { $_ -is [array] }
}
或类似的东西。这有点取决于你如何解析向量。出于此任务的目的,您当然可以向属性添加属性,无论它们是否具有向量。
答案 1 :(得分:0)
另一种可能性是将文件解析为多行字符串:
[regex]$filter_match =
@'
(?ms)\s*BEGIN STEP (\d+)\s*
\s*BEGIN ATTRIBUTES\s*
\s*name=Filter\s*
.+?
\s*(END STEP \d+)
'@
$vector_match =
@'
(?ms)\s*BEGIN VECTOR val_string val_string_pos\s*
\s*\[1\]=Abc\s*
\s*\[1\]=0\s*
\s*END VECTOR\s*
'@
$text = get-content testfile.txt -raw
$filter_match.Matches($text) |
foreach {
$x = $_.groups[1].value
$y = $_.groups[2].value
$HasVector = $_.groups[0].value -match $vector_match
$x
$y
$HasVector
}
5
END STEP 5
True