File Processing - Extract a Portion of a File

I'll bet there is a way to do this without having to examine a file line-by-line.
I want to read in a file that has data in this format:
- pre-data
- key-phrase
- post-data

I want to exclude all 'pre-phrase' data and create a new file that has only the key phrase and post-data. Thus, given the following file contents:

this is pre-data
this is also pre-data
and so is this
Once upon a time, in a land far, far away
there was a fairy princess ....

If the key-phrase is "once upon a time" then the output would look like:
Once upon a time, in a land far, far away
there was a fairy princess ....

Some givens:
- The key-phrase may be preceded by whitespace (tabs or spaces) but other than that will always be at the start of a new line
- I know I can read the file using $file = Get-Content "c:\temp\file.txt" -Raw
- I know I can write the contents out to a new file using $file | Out-File -FilePath "c:\temp\newfile.txt" -Force
- What I need to know is how to exclude everything preceding the key-phrase.
LVL 15
dbbishopAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
How far does post-data extend?

this is pre-data
 this is also pre-data
 and so is this
 Once upon a time, in a land far, far away
 there was a fairy princess ....
is this post-data?
how about this?
and this?

Would it go all the way to the last line, or just the line after "once upon a time"?
0
footechCommented:
Basically you can adjust how many lines are post-data by changing the -context parameter.
Select-String -Path ".\#ee-sample1.txt" -Pattern "once upon a time" -Context 4 |
 ForEach {$_.line; $_.context.postcontext} |
 Out-File newfile.txt

Open in new window

0
dbbishopAuthor Commented:
Actually, until after defined end-keyword (inclusive). So, if the end-keyword was 'the end' and the text contained:
this is pre-data
 this is also pre-data
 and so is this
 Once upon a time, in a land far, far away
 there was a fairy princess ....
...
the end
Index
apples...1
bananas....2
...
in this case I would want to capture everything from key-phrase (inclusive) up to end-keyword (inclusive).
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

dbbishopAuthor Commented:
Also, there could be hundreds of lines following the key-phrase.
0
dbbishopAuthor Commented:
footech: thanks for bringing that up. I suppose in my case, I can make -Context be 1000 (that should be enough) but now the challenge is how to make it 'stop' after reading the end-keyword?
0
footechCommented:
Since the number of lines can vary, I'll change the approach.
Get-Content ".\#ee-sample1.txt" | ForEach -Begin { $capture = $false } -Process `
{
    If ($_ -match "once upon a time")
    { $capture = $true }
    If ($capture)
    {$_}
    If ($_ -match "the end")
    { $capture = $false }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dbbishopAuthor Commented:
Works like a charm!!! There needs to be an A++ grade! :-)
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.