我知道powershell(足以让我离开);然而,我遇到了一个问题,我甚至无法在谷歌找到答案..
我有这个包含
的test.audit文件(例如)#####################################
# test here blah blah
# text
# 1.23
# sample
#####################################
<check_type>
# 1.2
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.1 text blah blah"
</custom_item>
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.2 other text blah blah"
solution : "Set the max_log_file data and equal <mb>"
</custom_item>
# 1.4
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.1 more text blah blah"
</custom_item>
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.3 text blah blah"
</custom_item>
</check_type>
使用该test.audit文件..我试图删除整个&#34; custom_item&#34;包含&#34; 8.1.1.1&#34;。
描述的字符串所以新文件看起来像
#####################################
# test here blah blah
# text
# 1.23
# sample
#####################################
<check_type>
# 1.2
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.2 other text blah blah"
solution : "Set the max_log_file data and equal <mb>"
</custom_item>
# 1.4
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.3 text blah blah"
</custom_item>
</check_type>
我有人帮助我,但是他们试图使用&#34; XML&#34;技术通过powershell ..
但是,由于某些原因,这不起作用.. 它包含一个&#34;#&#34; 它也有&#34;&#34;在里面抛出一个错误。还有一个解决方案
#$text = gc c:\temp\test.audit | Out-String
$text = @'
<check_type>
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.1 text blah blah"
</custom_item>
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.2 other text blah blah"
</custom_item>
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.1 more text blah blah"
</custom_item>
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.3 text blah blah"
</custom_item>
</check_type>
'@
[string]$text = $text -replace '<custom_item>', '=' -split '=' | ? {$_ -notmatch '8.1.1.1'}
$text = $text.Split("`n") | % {if ($_ -match '^\s+$') {' <custom_item>'} else {$_}}
$text #| out-file c:\temp\test.audit
但是这会导致文档非常混乱..不像行一样
如果有人可以提供帮助,我们将不胜感激。
感谢
答案 0 :(得分:1)
您可以使用RegEx匹配并替换以清除它。如果您想了解以下内容的工作原理,请查看RegEx101上的this解释。它确实输出了你要求的东西我很确定。
$text = @'
#####################################
# test here blah blah
# text
# 1.23
# sample
#####################################
<check_type>
# 1.2
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.1 text blah blah"
</custom_item>
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.2 other text blah blah"
solution : "Set the max_log_file data and equal <mb>"
</custom_item>
# 1.4
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.1 more text blah blah"
</custom_item>
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.3 text blah blah"
</custom_item>
</check_type>
'@
$text -replace "(?s)[\r\n]*<custom_item>[^\/]*?8\.1\.1\.1.*?<\/custom_item>"
由于您无法使用-Raw
(意味着您最有可能使用PowerShell v2),您可以这样做:
$text = (Get-Content "\the\path\here") -join "`r`n"
$text -replace "(?s)[\r\n]*<custom_item>[^\/]*?8\.1\.1\.1.*?<\/custom_item>" | Out-File "\the\path\here"
当我在我的机器上运行它时,它提供了这个输出(几乎就是你要求的):
#####################################
# test here blah blah
# text
# 1.23
# sample
#####################################
<check_type>
# 1.2
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.2 other text blah blah"
solution : "Set the max_log_file data and equal <mb>"
</custom_item>
# 1.4
<custom_item>
system : "Linux"
type : "CHECK"
description : "8.1.1.3 text blah blah"
</custom_item>
</check_type>