Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Powershell to find multi-line patterns in files

How would I find a multiline pattern in files, such as the contents of an XML node, using Powershell?

i.e. if I were looking for the word "green" within the deviceDescription node, but the XML node text may span multiple lines, this doesn't work:

dir -r -i *.xml | select-string -Pattern "<deviceDescription>.*green.*</deviceDescription>"
like image 974
Jeff Meatball Yang Avatar asked Nov 10 '11 15:11

Jeff Meatball Yang


1 Answers

First of all, if is xml, extract the device description string treating it as such and then match for the string you want, in this case, green.

$x = [xml] (gc .\test.xml)
$x.deviceDescription -match "green"

If you don't want to go that way, you will have to use the ?s - singleline or dotall flag which makes * match newlines:

$x = [IO.File]::ReadAllText("C:\test.xml")
$x -match "(?s)<deviceDescription>.*green.*</deviceDescription>"

Note that you probably want to use .*? or this may span across multiple deviceDescription tags. Edge cases like this are reasons why you should avoid using regex for such things.

like image 166
manojlds Avatar answered Oct 24 '22 01:10

manojlds