202007042620070426 83100 501
202007053120070531 99520 501 202007053120070531 53011 202007053120070531 4010 202007053120070531 2724
202007051520070515 4241 202007051520070515 4019 202007051520070515 4439
RAW-sample.txtGet-Content C:\Users\ltorres\Documents\Powershell\SampleDataRAPS.txt | select `
@{n='POS' ; e= {-join $_[ 0.. 1]}},
@{n='Start'; e= {-join $_[ 2.. 9]}},
@{n='End' ; e= {-join $_[10..17]}},
@{n='Code' ; e= {-join $_[19..23]}},
@{n='msg' ; e= {-join $_[26..28]}},
@{n='POS' ; e= {-join $_[33.. 34]}},
@{n='Start'; e= {-join $_[35.. 42]}},
@{n='End' ; e= {-join $_[43..50]}},
@{n='Code' ; e= {-join $_[51..55]}},
@{n='msg' ; e= {-join $_[56..58]}}
Get-Content Raw-sample.txt | % {
$line = $_
while ($line[0] -ne ' ') {
-join $line[0..29]
if ($line.Length -gt 32) {$line = $line.Remove(0,32)} else { $line = ' ' }
}
} | select `
@{n='POS' ; e= {-join $_[ 0.. 1]}},
@{n='Start'; e= {-join $_[ 2.. 9]}},
@{n='End' ; e= {-join $_[10..17]}},
@{n='Code' ; e= {-join $_[19..23]}},
@{n='msg' ; e= {-join $_[26..28]}} | export-csv -NoType Formatted.csv
Footech, having to use the regex twice does indeed look suspicious.
$pattern = "(?<POS>[0-9]{2})(?<Start>[0-9]{8})(?<End>[0-9]{8}) (?<Code>[0-9]{4,7}) +(?<Msg>[0-9]{3,7}(?![0-9]))?"
gi .\RAW-sample.txt | Select-String -Pattern $pattern -AllMatches | % {$_.matches} |
% { New-Object PsObject -Property @{
POS = $_.groups["POS"].value
Start = $_.groups["Start"].value
End = $_.groups["End"].value
Code = $_.groups["Code"].value
Msg = $_.groups["Msg"].value
}
} | Select POS,Start,End,Code,Msg |
Export-CSV result.csv -notype
$match = $pattern.match((get-content RAW-sample.txt))
$(
while ($match.Success)
{
#stuff
}
$match = $match.NextMatch()
}
)
Open in new window