So, we don’t use the [code] tag; it’s an HTML PRE tag. But, XML in particular - because browsers see it as HTML - doesn’t render well in the forums. Make a Gist out of your XML, and then paste the Gist URL here, and we’ll display it properly. There’s a link to Gist right above the reply textbox, and it just uses your GitHub account.
So, that is “valid” XML, but it isn’t especially well designed. All PowerShell is going to let you do easily is get to the custom_item elements. From there, it’s just going to see the contents alone undifferentiated string. That means probably using a -replace operator to replace the text with an empty string. Honestly, I’m not even sure it’s worth treating this as XML, given what you want to do. It might be easier to use Get-Content to read it line by line, and just replace what you want, and then output it to a new file.
So, read each line. For each line, see if the line contains the search text. If it does, replace the search text with something else (maybe you’d use a blank string?). Let the output fall through to a new file.
Sorry if I am not explaining this right… but if you were to look back at the xml code it has
Custom_item (in xml form)
system : linux
description : 8.1.1.1
(close)Custom_item (in xml form)
Custom_item (in xml form)
system : linux
description : 8.1.1.2
(close)Custom_item (in xml form)
Custom_item (in xml form)
system : linux
description : 8.1.1.1
(close)Custom_item (in xml form)
after running the script it should look like
Custom_item (in xml form)
system : linux
description : 8.1.1.2
(close)Custom_item (in xml form)
the entire record was deleted… I don’t want to delete just “custom_item” nor do I want to delete just “description”… the entire entry should be deleted. containing custom_item, description, system, etc… if description contains 8.1.1.1
Oh. No, I wasn’t getting that. Yeah, that’s harder. Honestly, I’d basically have to write a parser for you that read in the file, looked for section beginnings, stashed the whole section in a variable, analyzed it, and output it if it wasn’t to be deleted. Not technically difficult, but it’s not something I’m going to be able to do for you right here. Sorry :(.
Well, “hard” depends on your experience with programming. I don’t think it’s technically hard at all; it’s just not something I can hack out for you in 1 minute.
When you see a start-of-section line, you start adding all subsequent lines to a variable, until you see an end-of-section line. When you see end-of section, evaluate the contents of the variable, decide if you’re going to output it or not, clear the variable, and keep going.
You can’t do it easily as a one-liner, because you’ve got to have some variables to track what you’re doing. Like, “currently accumulating lines and waiting for end-of-section tag.”
With the xml you provided the system, type, and description is a single text item contained in each custom_item and the code does remove each one if the text ‘description : "8.1.1.1’ is found. What the sample code does not do is write the results back to the file, it only changes the variable in memory. Here is a more complete example.
I do not know of any specific tutorials to point you to for parsing but Don laid out the steps you would have to take.
[xml]$xml = gc d:\test\test.txt
Write-host "Before"
$xml.check_type.ChildNodes.'#text'
Write-host "-----------------------------------------------"
foreach($child in $xml.check_type.ChildNodes){
if($child.'#text' -match 'description : "8.1.1.1'){$child.RemoveAll()}
}
Write-host "After"
$xml.check_type.ChildNodes.'#text'
Write-host "-----------------------------------------------"
$xml.Save("d:\test\test2.txt")
[xml]$xml2 = gc d:\test\test2.txt
Write-host "From new file"
$xml2.check_type.ChildNodes.'#text'
Before
system : "Linux"
type : "Check"
description : "8.1.1.1 this is other text"
system : "Linux"
type : "Check"
description : "8.1.1.1 more text"
system : "Linux"
type : "Check"
description : "8.1.1.2 this is other text"
-----------------------------------------------
After
system : "Linux"
type : "Check"
description : "8.1.1.2 this is other text"
-----------------------------------------------
From new file
system : "Linux"
type : "Check"
description : "8.1.1.2 this is other text"
I’m not too up to speed on what XML should look like, but as an alternative to the brute force string manipulation maybe you could have a script that cleans up the junk xml into clean and proper xml, then you can deal with it that way. Might or might not be any easier, just an idea.