Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2562

Regex help- Extracting info from txt file

$
0
0

Hello!

I have this info:

Bears1
Bears2
Apple1: Bears3
Bears4
Apple2
Apple3
Apple4: Bears5
Apple5: Bears6

I would like to extract just the info where it says :

Bears1
Bears2
Bears4

Not anything with Apples1, Apples2, etc and not the info where Bears is in the same line. So not any of these:

Apple1: Bears3
Apple2
Apple3
Apple3: Bears5
Apple4: Bears6

The final Output should just be:

Bears1
-------------
Bears2
-------------
Bears4

I have been using this:

Function Extract{
$file1 = "C:\Users\temp.txt"
$file2 = "C:\Users\temp.txt"

$startValue = '^Bears'
$innerValue = '^Bears'

$regex4 = "(?m)\A(?=$startValue)|\r\n(?=$startValue)"
$regex5 = "(?m)$innerValue"

$content = [IO.File]::ReadAllText($file1).TrimEnd()

# Split the string, filter in if inner value matches and exclude 1st element
$result = $content -split $regex4 | Select-Object -Skip 1 | Where-Object { $_ -match $regex5}

# Write to disk
$result -join "`r`n$('- ' * 13)`r`n" | Set-Content -Path $file2
}
Extract

Also this is a generic example so the real data is more complex however the Regex commands here should not be different when matching.

My error is that it will match the cases where Bears is inside the line that begins with ApplesX. So what is matched is this (Which I do not want):

Bears1
-------------
Bears2
-------------
Apple1: Bears3
-------------
Bears4
-------------
Apple4: Bears5
-------------
Apple5: Bears6
-------------

I am having trouble (but no shown error in the shell)  with the command here:

$startValue = '^Bears'
$innerValue = '^Bears'

Thanks! 


Viewing all articles
Browse latest Browse all 2562

Latest Images

Trending Articles



Latest Images