Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I split a block of text into multiline records based on the starting line?

How can I split this into separate strings/arrays/etc so that I can then iterate on each one separately? The goal is to separate them so that I can then parse each record and insert into a PSCustomObject.

I have the following block of text representing more than 1 scan. Each scan record begins with ‘Root Scan Path’ line and ends with ‘Suspicious Files’ line.

The closest I have been able to get is the following line, which works but then I lose the ‘Root Scan Path’ line because it’s the delimiter.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$String -Split "Root Scan Path: C:\\"

There could be more than 2 scan records as well.

Root Scan Path: C:\
Scan ID: 03307ad8-1a59-7d4e-8dfe-28864c9d1bc2
Status: Completed
Initiated from: User
Start Time: Monday, October 23, 2023 9:24:18 PM
End Time: Monday, October 23, 2023 9:34:00 PM
Scanned Files: 38311
Unsupported Files: 229479
Traversed Files: 321202
Total Files: 267790
Suspicious Files: 0
Root Scan Path: C:\
Scan ID: f32a64b0-56f5-664c-bf37-a7b120f759d8
Status: Running
Initiated from: User
Start Time: Tuesday, October 24, 2023 6:20:45 PM
End Time:
Scanned Files: 0
Unsupported Files: 0
Traversed Files: 0
Total Files: 0
Suspicious Files: 0

>Solution :

Assuming there are no empty rows between each block, the following might work:

$out = [ordered]@{}
$theInputString -split '\r?\n(?=Root Scan Path:)' | ForEach-Object {
    foreach ($line in $_ -split '\r?\n') {
        $key, $value = $line.Split(':', 2).Trim()
        $out[$key] = $value
    }
    [pscustomobject] $out
    $out.Clear()
}

I would recommend Olaf’s using ConvertFrom-StringData for its simplicity, however the cmdlet has a caveat that it doesn’t preserve the order of the properties (because hashtables don’t ensure its keys order), also another caveat I have noticed is that it consumes the backslash \ from the string (unsure if this is documented):

'Root Scan Path=C:\' | ConvertFrom-StringData

Name                           Value
----                           -----
Root Scan Path                 C:
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading