Okay, this will be probably a long shot, but I want to understand how it works.
I have a bunch of files that has partially an xml format - since these are not real xmls, Select-Xml and linked cmdlets are not viable options .
However I am looking for some “elements” in the files, for this I am using the script of:
This is actually brilliant. While I am still digesting what magic was done with the RegEx replacement, could you advise how to remove the duplications on the filename “column”?
I tried
FileName = $_.Filename | Get-Unique
and
FileName = $_.Filename | sort -Unique
so far, but giving the same results:
So basically one filename could be enough per Pattern pairs
There are no duplicates. You have two separate independent patterns. Each pattern will produce a match. So you have in one file one match for the pattern <logicalIdentifier> and one match for the pattern <status>.
Depending on what you need the data for you could use
Here are two other options. I think what you’re trying to get is an object with the filename, the ID, and the status. Please correct me if I am incorrect.
Hello krzydoug and thank you for sharing your ideas.
I run both scripts and indeed they also give what I was looking for.
Just a few things to clarify for me:
in the first one, the pattern contains both, the ID and the status, handled as one string, am I correct? How does the script separate these?
Identifier = $_.groups['Identifier'].value does that mean that the “Identifier” column displays the RegEx value of the relevant pattern?
In this part in the second script:
You are using a subxpression ‘(?=<)’, but I am not sure why. You already declared where to look for: (?<Status>.+?) - I am really not familiar with the regex approach
I used the regex class matches method, which will find all occurrences of matching text.
I used named “capture groups” for readability. Iterate the captures, pull the groups object (like a dictionary) and pull the ‘Identifier’ property. It’s only named that because of the named capture group.
The pattern matches <status> and then any number of characters .+ but the shortest amount of characters ? that’s what this pattern here is combined .+? Without some stopping marker, it would match the rest of all the text. This says match until you encounter the character <
I encourage you to check out this regex demo and also use this site to learn more about regex. Regex can be used almost everywhere, so it is beneficial outside of powershell as well.