0

Using Powershell I would like to extract a value from text file that is in between two lines that match a pattern.

I'm trying to match 3 lines, 1st & 3rd will always be the same:

1st: '  1'
2nd: trying to read... always 2-4 characters
3rd: ' 40'

There are multiple occasions where line 1&3 should match this.

I tried with bellow code.

$aa=Get-Content $filename1 -Raw
$aaa=$aa  |Where-Object { ( $_ -match '(.\s1)(?:\r\n|[\r\n])*(?:\r\n|[\r\n])(\s40)') }
$aaa

I get too much output...maybe it's matching just 1st and 3rd line and many lines in between.

Robert Dyjas
  • 4,979
  • 3
  • 19
  • 34

2 Answers2

0

Regex is usually a poor substitute for a context-sensitive multi-line parser.

Given the document format, I'd simply write one:

$grabLine = $false

switch -File($filename1){
  '  1' {
    $grabLine = $true
  }
  ' 40' {
    $grabLine = $false
  }
  default{
    if($grabLine){
      $_
      # break here if you only need one line 
    }
  }
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
0

You can do it using Regex:

$regex = [regex] '\s+1\r?\n(?<secondline>.*)\r?\n\s+40'
$match = $regex.Match($text)
$result = while ($match.Success) {
    $match.Groups['secondline'].Value
    $match = $match.NextMatch()
} 

$result

Where $text is the file you read with $text = Get-Content 'FILENAME' -Raw like this:

  1
trying to read... always 2-4 characters
 40
  1
another second line
 40
  1
the line you are interested in
 40

The result is

trying to read... always 2-4 characters
another second line
the line you are interested in
Theo
  • 57,719
  • 8
  • 24
  • 41