0

I have a text file that i need to read the lines in between a pattern and include the pattern match.

Example:

Attributes
att 1
att 2
col 1
col 2

Attributes
att 1
att 2
pos 1

Attributes
set 1
set 2
att 1

Right now I have:

$Attributes = Get-Content att.txt | Select-String "Attributes" -Context 0,3

Which returns the attributes and the next 3 lines into an array entry. However, I would like this to be dynamic and not specifically the next 3 lines. I would like all lines until the next attributes pattern.

catalyph
  • 101
  • 9
  • 1
    Is this somehow different than [the last question you asked](https://stackoverflow.com/questions/45927459) that you just left hanging? Also, the example text you give does not match the code that you supplied. Nowhere does "DBServer" appear in the example text. Regardless, the `Select-String` cmdlet isn't dynamic like you want, and really isn't the right thing to use for this case. My answer on the other question shows how to split the file to get the chunks of data you want. – TheMadTechnician Aug 29 '17 at 18:37
  • 1
    Split the text at 2+ consecutive line breaks. – Ansgar Wiechers Aug 30 '17 at 01:20
  • Yes the other question was for the syntax i need to split the string after the = and before the \n and only get the string result. This is how do i get any number of lines as variables including the Attributes line and each inbetween the sets of attributes. Some attribute sets will have 5 lines and some will have 8 lines , some will have 4 – catalyph Aug 30 '17 at 13:06
  • There wont always be 2 or more line breaks. it might be 3 it might be 5 it might be 0 – catalyph Aug 30 '17 at 18:49
  • Still looking for anything that might work for this – catalyph Aug 31 '17 at 15:31
  • Try String.Split() method https://blogs.technet.microsoft.com/heyscriptingguy/2014/07/17/using-the-split-method-in-powershell/ – Dima Fomin Dec 25 '18 at 08:51

0 Answers0