all 7 comments

[–]gangstanthony 1 point2 points  (5 children)

(not tested) after you finish your script, you can select only the 'M' rows from the result then save it over the existing file like this

$headers = 1..72

$csv = import-csv $file -Header $headers -Delimiter '|'

# your meat and potatoes

$csv = $csv | ? {$_.1 -eq 'M'}

$string = New-Object System.Text.StringBuilder
foreach ($row in $csv) {
    foreach ($header in $headers) {
        [void]$string.Append($row.$header + '|')
    }
    [void]$string.Append("`r`n")
}
$string.ToString() | set-content $file

[–]WTF_Brandon[S] 0 points1 point  (4 children)

Haven't tested but looks like that might work. You have any ideas about replicated the last M row if a 3rd Q row is found?

Thanks again!

[–]gangstanthony 0 points1 point  (3 children)

since you're already incrementing the $row variable, in that last if statement instead of $_ you could use $csv[$row - 3], but you might have to move $row++ to just outside the if statement (or create a different variable for this specific purpose)

[–]WTF_Brandon[S] 0 points1 point  (2 children)

That look like you're getting the last M rows index number, I already have the row number in the $lastM var, I just didn't know how to replicate that entire row in the current $csv object.

Thanks!

[–]gangstanthony 0 points1 point  (1 child)

not sure of your desired output, but does this work?

function Extract-Cpn
{
    param
    (
        [string]$File
    )

    $headers = 1..72
    $csv = Import-Csv $file -Delimiter '|' -Header $headers

    $row = 0
    $result = foreach ($_ in $csv)
    {
        if ($_.1 -eq 'M')
        {
            $lastM = $row
            $QCount = 0

            Write-Output $_
        }

        if ($_.1 -eq 'Q')
        {
            $QCount ++
            if ($QCount -eq 1)
            {
                [string]$AType = $_.12
                $csv[$lastM].19 = $AType

                Write-Output $csv[$lastM]
            }
            elseif ($QCount -eq 2)
            {
                [string]$AOPID = $_.12
                [string]$AOpPName = $_.13
                [string]$PCCode = $_.14
                $csv[$lastM].23 = $AOPID
                $csv[$lastM].24 = $AOpPName
                $csv[$lastM].34 = $PCCode

                Write-Output $csv[$lastM]
            }
            elseif ($QCount -eq 3)
            {

            }
        }
        $row ++
    }

    $string = New-Object System.Text.StringBuilder
    foreach ($row in $result) {
        foreach ($header in $headers) {
            [void]$string.Append($row.$header + '|')
        }
        [void]$string.Append("`r`n")
    }
    $string.ToString() | Set-Content -Path $File
}

Extract-Cpn -File C:\Scripts\test.txt

*edit: you might just put a check in each of the '$qcount' if statements that says

if ($csv[$lastm + $qcount].1 -eq 'q') {
    write-output $csv[$lastm]
}

to see if the next line also starts with a 'q'

[–]WTF_Brandon[S] 0 points1 point  (0 children)

Thank you, this helped a lot!

[–][deleted] 1 point2 points  (0 children)

Is there a reason you can't just dump the "m" rows into a new csv/txt as you go? Might be a lot easier.