Reputation: 51
I know powershell (enough to get my by); however, i have ran into an issue in which i cant even find the answer on google..
I have this test.audit file (for example) that contains
#####################################
# 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>
Using that test.audit file.. I am trying to delete the entire "custom_item" string that contains a description of "8.1.1.1".
So the new file would look like
#####################################
# 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>
I had someone help me however they tried to use the "XML" technique through powershell..
however, this will not work for several reason.. it contains a "#" and it also has "" inside it which throws an error.
There was also one more solution
#$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
however this outputs the document to be very messy.. not line for line like it was
If someone could assist that would be appreciated.
thanks
Upvotes: 1
Views: 51
Reputation: 36332
You can use a RegEx match and replace to clear this up. Check out this explaination on RegEx101 if you want to see how the following works. It does output exactly what you are requesting I'm pretty sure.
$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>"
Since you can not use -Raw
(meaning you have v2 of PowerShell most likely), you can do this:
$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"
When I ran that on my machine it provided this output (almost exactly what you asked for):
#####################################
# 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>
Upvotes: 1