Multiline regex match in PowerShell -


i'm trying extract block of lines text file, contains this:

... scountry = "usa" scity = "new york" sstate = "new york" ... scountry = "usa" scity = "los angeles" sstate = "california" 

where 3 lines repeat throughout text file; want extract lines of text, , put data fields csv, have like

"usa","new york","new york" "usa","los angeles","california" ... 

so far have this:

$inputpath = 'c:\folder\file.vbs' $outputfile = 'c:\folder\extracted_data.csv' $filecontent = [io.file]::readalltext($inputpath)  $regex = '(?sm)(s[a-z][a-z]+ = "\w*"(\s*$)){3}'  $filecontent = $filecontent | select-string $regex -allmatches | % {$_.matches} | % {$_.value} $filecontent = [regex]::replace($filecontent, 'scountry = ', '') $filecontent = [regex]::replace($filecontent, '(?sm)((^\s*)s[a-z][a-z]+ = )', ',') $filecontent > $outputfile 

which able obtain looking @ this:

multiline regex match config block.

however, output file empty when run script. won't pattern-match $regex pattern provided, match on single line if like:

$regex = '(?sm)(scountry = "\w*"(\s*$))' 

but not if like:

$regex = '(?sm)(s[a-z][a-z]+ = "\w*"(\s*$))' 

how make pattern matching work across multiple lines?

using test data exactly have in post took different approach using select-string , convertfrom-stringdata. has minor flaw overlooked (or addressed if need to). caveat here scountry line has occur first , sstate line has occur last in group.

$results = ((get-content c:\temp\test.txt -raw) |      select-string -pattern "(?sm)scountry.*?sstate.*?$" -allmatches).matches.value $results | foreach-object{     new-object -typename pscustomobject -property ($_.replace('"','') | convertfrom-stringdata) } | export-csv -notypeinformation c:\temp\output.csv 

to data groups regex here grab "scountry" end of line of next "sstate" occurs. current logic fail if there other lines in between besides ones expected. rid of variable quotes simple .replace('"',''). think minor resulting headers have leading s's not big deal.

the object in powershell looks before exported csv

scity       scountry sstate     -----       -------- ------     new york    usa      new york   los angeles usa      california 

which net output in csv

"scity","scountry","sstate" "new york","usa","new york" "los angeles","usa","california" 

the cool think not sort data before exported it. or powershell objects.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -