0

The data being pulled from this.

<IPv4 id="id456F07894780" name="XYZ" comment="stuf123" ro="False"      address="1.1.1.1" netmask="255.255.255.255"/>

Here is my PS script.

$input_path = ‘c:\ps\search\fwbuilder.txt’
$output_file = ‘c:\ps\extracted_ip_addresses.txt’
$regex = '((?<=\bname="\b).*?(?=\".comment\b)|(?is)(?<=\baddress="\b).*?(?=\".netmask\b))'
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file

I want the display to be like this.

XYZ 1.1.1.1

But instead it is showing up as

XYZ

1.1.1.1 EDIT*************

I was able to accomplish what I needed using the XML parser in PS.

Get-Content -Path C:\ps\search\network.xml | Select-Xml -XPath //Network | Select-object -ExpandProperty "node" | Select-object "name","address","netmask","id"

Thanks for the help.

1 Answers1

2

Not that it can't be done using regex but you might be complicating things to much. (and regex really isn't the right tool for the job

If your input is valid xml, I would use following approach

[xml]'<IPv4 id="id456F07894780" name="XYZ" comment="stuf123" ro="False" address="1.1.1.1" netmask="255.255.255.255"/>' | 
ForEach-Object {"$($_.IPv4.name) $($_.IPv4.address)"}

Output

XYZ 1.1.1.1

Applied to your files, following might be all that's needed

[xml](Get-Content c:\ps\search\fwbuilder.txt) | ForEach-Object {
    "$($_.IPv4.name) $($_.IPv4.address)" 
} | Out-File c:\ps\extracted_ip_addresses.txt

Edit

as per your comments, the txt file doesn't contain a root element, you might add one on the fly as follows

([xml]"<root>$(Get-Content c:\ps\search\fwbuilder.txt)</root>").root.IPv4 | ForEach-Object { 
    "$($_.name) $($_.address)" 
}
Community
  • 1
  • 1
Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
  • I like your answer, but I did go down the XML route and wasn't able to get any luck. I tried your example and it. Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument". Error: "The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type." – Alex Huthmacher Mar 14 '17 at 06:12
  • @AlexHuthmacher - I assume you are talking about the `get-content` example?! First thing that comes to mind is that fwbuilder.txt doesn't contain valid xml. Can you post a minimal content of the file that still returns that exception? – Lieven Keersmaekers Mar 14 '17 at 06:19
  • So I think it is because there are multiple lines in the file. When I just have one line it works, but if I add another line it fails. works but does not. – Alex Huthmacher Mar 14 '17 at 06:24
  • You'd have to add a root element. I'll adjust my example. – Lieven Keersmaekers Mar 14 '17 at 06:48
  • It turns out all I had to do was this. Get-Content -Path C:\ps\search\Ipv4.xml | Select-Xml -XPath //IPv4 | Select-object -ExpandProperty "node" | Select-object "name","address","netmask","comment" – Alex Huthmacher Mar 14 '17 at 17:59
  • @AlexHuthmacher - You can post your solution and accept it as answered. Others might benefit from it. – Lieven Keersmaekers Mar 15 '17 at 05:47