Powershell: How to match a string in a specific position in text file?

Hello Experts,

I am looking for some assistance with searching for a match against a specific string in a text file using Powershell. I have researched numerous methods so far and can't seem to determine how to satisfy this criteria:

 Sample data
I need to look for the string '20180125', and export the entire line to a new file ONLY when that string is found in the position in Green. If it is located at the red position, I don't care about the line. (But I DO care if it i located in both positions...)

I've tried some REGEX variations but I am having difficulty. I thought perhaps if I used Search-String with a pattern that it should suffice, I just can't figure out what that pattern needs to look like to match '20180125' only when it exists in the position in GREEN.
LVL 15
Matt MinorTechnical Systems Analyst IIAsked:
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.

oBdACommented:
The pattern is fairly easy, you just have to anchor it to 14 preceding digits. The following should do the trick; if it doesn't, post some actual sample lines inside a [code][/code] box, not a screenshot.
'\ASQ.*?\s+\d{14}20180125'

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
Matt MinorTechnical Systems Analyst IIAuthor Commented:
Seeing that makes perfect sense now - I tried using the anchor yesterday but used () instead of {} so clearly the result was incorrect.

May I ask what the first part of the pattern defines? '\ASQ.*?

Thank you so much! Your quick assistance is very appreciated.
0
Matt MinorTechnical Systems Analyst IIAuthor Commented:
Nevermind, I figured it out using a regex tester. A sample line returns correctly, thank you so much again.
0
Joe FulginitiNetwork EngineerCommented:
Does something like this work?

foreach($line in Get-Content c:\temp\test.txt) {
    
    if($line -like '*12232211*'){
       # Line contains 12232211 and now we will print that line to the screen
        $line   
    }
}

Open in new window

0
Matt MinorTechnical Systems Analyst IIAuthor Commented:
Pattern provided by oBdA is exactly what I was missing. Thank you!
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.