I'm trying to extract a certain block of lines from a text file, which contains something like this:
...
sCountry = "USA"
sCity = "New York"
sState = "New York"
...
sCountry = "USA"
sCity = "Los Angeles"
sState = "California"
where those three lines repeat throughout the text file; I just want to extract those lines of text, and put the data fields into a csv, so that I have something like
"USA","New York","New York"
"USA","Los Angeles","California"
...
So far I 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 I was able to obtain by looking at this:
Multiline regex to match config block.
However, my output file is empty when I run the script. It won't pattern-match with the $regex
pattern I provided, but it WILL match on a single line if I do something like:
$regex = '(?sm)(sCountry = "\w*"(\s*$))'
but not if I do something like:
$regex = '(?sm)(s[A-Z][a-z]+ = "\w*"(\s*$))'
How do I make the pattern matching work across multiple lines?