-2

My powershell script doesn't seems to work to extract the data between multiple keywords spread across various lines. I want to select all the lines after NAME1 till NAME2..6 appears.So all the data between NAME1 & NAME2/3/4/5/6. The keyword NAME1 appears in the file multiple times so does other keywords. The regex however works but i am unable to figure out the mistake or the approach that i am taking when i use the regex in powershell script.

(Get-Content .\withoutStar.txt | Where-Object {$_ -match '(?is)(?<=\NAME1\b).*?(?=\bNAME2\b|\bNAME3\b|NAME4|NAME5|NAME6\b)'}) | Set-Content .\AllfunctionGroup.txt

The sample data and regex can be tested here.Sample data and regex

Can anyone throw light on this what is missing in my script.

Community
  • 1
  • 1
n00b
  • 181
  • 1
  • 1
  • 18
  • I think that with trying to match multiple lines it may be easier to use something like a for loop so you can match on a line and keep adding each line or match to a variable until your code logic tells it to stop. – Jason Snell Aug 10 '17 at 14:36
  • What kind of output would you like to see? – ArcSet Aug 10 '17 at 14:45

1 Answers1

1

The problem with your script is here :

Where-Object {$_ -match '(?is)(?<=\bNAME1\b).*?(?=\bNAME2\b|\bNAME3\b|NAME4|NAME5|NAME6\b)'}
This doesn't mean "keep the matches", but "if there is a match, keep the entire string".

Here is another way to do what you want :

$text = Get-Content .\withoutStar.txt
$regex = "(?is)(?<=\bNAME1\b).*?(?=\bNAME[2-6]\b)"
Set-Content .\AllfunctionGroup.txt -Value ""
(Select-String -Pattern $regex -Input $text -AllMatches).Matches | ForEach {
    Add-Content .\AllfunctionGroup.txt $_.Value
}

Edit : I simplified a bit your regex

Gawil
  • 1,171
  • 6
  • 13