gpt4 book ai didi

powershell - 在避免多行数组powershell行为的同时,可以按字符串将该文件分成多个部分吗?

转载 作者:行者123 更新时间:2023-12-03 00:59:07 26 4
gpt4 key购买 nike

我有DISA STIG的文本打印输出。看起来像这样:

Group ID (Vulid):  V-96855
Group Title: SRG-NET-000018-RTR-000001
Rule ID: SV-105993r1_rule
Severity: CAT II
Rule Version (STIG-ID): CISC-RT-000010
Rule Title: The Cisco router must be configured to enforce approved authorizations for controlling the flow of information within the network based on organization-defined information flow control policies.
...
_____________________________________________________________

Group ID (Vulid): V-96857
Group Title: SRG-NET-000025-RTR-000020
Rule ID: SV-105995r1_rule
Severity: CAT II
Rule Version (STIG-ID): CISC-RT-000020
Rule Title: The Cisco router must be configured to implement message authentication for all control plane protocols.
...
_____________________________________________________________

Group ID (Vulid): V-96859
Group Title: SRG-NET-000025-RTR-000085
Rule ID: SV-105997r1_rule
Severity: CAT II
Rule Version (STIG-ID): CISC-RT-000030
Rule Title: The Cisco router must be configured to use keys with a duration not exceeding 180 days for authenticating routing protocol messages.
...

我想对此进行处理,但是我遇到了Powershell如何处理多行输入的问题。我试图将每个规则(在____分隔符之间)分隔为自己的部分,然后将其转换为xml。当我导入内容并尝试对其进行拆分时,它要么一次出现一行,这意味着我不能很好地处理分隔符,或者我尝试将其编码为字符串,在这种情况下,每种角色一次出现。
    $rawContent = Get-Content C:\Users\ncfx\Projects\SignatureRWK\raw.txt -Raw

$splitRules = $rawContent.Split("_____________________________________________________________")

Foreach ($rule in $splitRules) {
$rulectArr = $rule.split(":")
$processedContent += @"
<Rule>
<Group ID>$($rulectArr[1])</Group ID>
#...
"@

所需输出:
<GroupId>V-96197</GroupId>
<GroupTitle>SRG-APP-000026-NDM-000208</GroupTitle>
<RuleId>SV-105335r1_rule</RuleId>

实际输出:
<GroupId>V-96197</GroupId>
<GroupTitle></GroupTitle>
<RuleId></RuleId>
...
<GroupId>SRG-APP-000026-NDM-000208</GroupId>
<GroupTitle></GroupTitle>
<RuleId></RuleId>

我也尝试过使用正则表达式,但没有取得很大的成功(尽管我将是第一个承认我在这些方面很不好的人)。

最佳答案

简洁的解决方案:

((Get-Content -Raw raw.txt) -split '\r?\n_+\r?\n\r?\n') | ForEach-Object { 
@"
<Rule>
$(
$(foreach ($line in $_ -split '\r?\n' -ne '') {
$name, $value = $line -split '(?: \(.+?\))?: +'
$name = $name -replace ' '
" <$name>$value</$name>"
}) -join "`n"
)
</Rule>
"@
}

使用您的样本输入,上面的结果是:
<Rule>
<GroupID>V-96855</GroupID>
<GroupTitle>SRG-NET-000018-RTR-000001</GroupTitle>
<RuleID>SV-105993r1_rule</RuleID>
<Severity>CAT II</Severity>
<RuleVersion>CISC-RT-000010</RuleVersion>
<RuleTitle>The Cisco router must be configured to enforce approved authorizations for controlling the flow of information within the network based on organization-defined information flow control policies.</RuleTitle>
<...></...>
</Rule>
<Rule>
<GroupID>V-96857</GroupID>
<GroupTitle>SRG-NET-000025-RTR-000020</GroupTitle>
<RuleID>SV-105995r1_rule</RuleID>
<Severity>CAT II</Severity>
<RuleVersion>CISC-RT-000020</RuleVersion>
<RuleTitle>The Cisco router must be configured to implement message authentication for all control plane protocols.</RuleTitle>
<...></...>
</Rule>
<Rule>
<GroupID>V-96859</GroupID>
<GroupTitle>SRG-NET-000025-RTR-000085</GroupTitle>
<RuleID>SV-105997r1_rule</RuleID>
<Severity>CAT II</Severity>
<RuleVersion>CISC-RT-000030</RuleVersion>
<RuleTitle>The Cisco router must be configured to use keys with a duration not exceeding 180 days for authenticating routing protocol messages.</RuleTitle>
<...></...>
</Rule>

说明:
  • -split '\r?\n_+\r?\n\r?\n'用分隔线(___...)将输入文件的全部内容分成几行,
  • 然后,ForEach-Object调用使用可扩展的here-string(@"<newline>...")从块中的各行创建<Rules>元素:
  • $_ -split '\r?\n' -ne ''将每个块分成几行,过滤掉空行。
  • $name, $value = $line -split '(?: \(.+?\))?: +'通过分隔符:后跟一个空格,将每一行分为名称和值,还可以在(...)中添加一个子串
  • $name = $name -replace ' '从名称中删除所有空格。
  • 可扩展字符串" <$name>$value</$name>"为当前行构造XML元素。
  • -join "`n"将所有XML元素行与换行符连接在一起;如果要使用适合平台的换行符序列而不是"`n"(仅适用于LF),请使用[Environment]::NewLine
  • 关于powershell - 在避免多行数组powershell行为的同时,可以按字符串将该文件分成多个部分吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58530727/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com