Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pull data from a document

Tags:

powershell

I have a text file that has data as follows:

07:30 - 07:45PMTS09526052 | Sev9 | Location| | Due: 12/23/2016
NON PC HARDWARE - TROUBLESHOOT SW
Complete this Job

These lines will show up multiple times throughout the document, with variations, such as the alphanumeric characters after the letters "PMT" on line one.

I need to find a way so whenever it finds "PMT" it pulls that and the next 9 symbols, drops it in a CSV, and then puts the "Complete this job" line in the next spot on the CSV.

I know I'll need to set the pattern as PMT[ST][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] (where $WLDir is the filename.):

Select-String $WLDir -pattern “PMT[ST][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]”

I can't figure out what my next move is to isolate that part and the line 2 lines down and set them as cell data in a CSV. Can anyone help? I apologize as it looks like I've not done my homework here, but I've been trying to use a previous question and some some Hey! Scripting Guy! as references, but I'm just stuck.

like image 896
Nate Avatar asked May 22 '26 08:05

Nate


1 Answers

Not 100% sure what the result should looks like but I consider you want a CSV with a Time, Topic and Status column:

Select-String $WLDir -pattern '(PMT[ST]\d{8})' -Context 0,2 | ForEach-Object {
    [PSCustomObject]@{
        Time = $_.Matches.Groups[1].Value
        Topic = $_.Context.PostContext[0]
        Status = $_.Context.PostContext[1]
    }
} | ConvertTo-Csv -NoTypeInformation

This will output:

"Time","Topic","Status"
"PMTS09526052","NON PC HARDWARE - TROUBLESHOOT SW","Complete this Job"
like image 77
Martin Brandl Avatar answered May 24 '26 02:05

Martin Brandl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!