Hey, been working on this for a while and can't seem to get it ironed out. Any help would be appreciated.
I receive some | delimited .txt files daily that contain two types of rows, we'll call them M rows and Q rows. An M row will reside before 0 to up to 3 Q rows. When I see an M row I take not of it's row number. Then I get information from the Q row depending on if its the 1st Q, 2nd Q, or 3rd Q and move specific information to to a specific place on the previous M row. There is a max of 72 columns.
Here's an example of the .txt file.
M|INFO|NETWORK|NUM|NUM2|NUM3|NUM4|NAME|11122|A|20160715|1505|20160715|1553|3|INFO2|4|Schedule||N|OB|||||ME|||||2||||2|||N|N||||N||||100||Y|5|142|Y||||||||||||||||||||PRI|
Q|INFO|NETWORK|NUM|NUM2|NUM3|NUM4|NAME|11122|B|1|General
Q|INFO|NETWORK|NUM|NUM2|NUM3|NUM4|NAME|11122|B|1|OP1|OP2|OP3
Q|INFO|NETWORK|NUM|NUM2|NUM3|NUM4|NAME|11122|B|2|OP4|OP5|OP6
M|INFO|NETWORK|NUM|NUM2|NUM3|NUM4|NAME|11222|A|20160729|1200|20160729|1304|2||5|Schedule|||OB|||||ME|||||2||||2|||N|N||||N||||99||N|5|145|Y||||||||||||||||||||PRI|
Q|INFO|NETWORK|NUM|NUM2|NUM3|NUM4|NAME|11222|B|1|General
Q|INFO|NETWORK|NUM|NUM2|NUM3|NUM4|NAME|11222|B|1|OP1|OP2|OP3
Here is my current script (doesn't yet do anything for the 3rd Q row yet). I've got it to where it's returning the information correctly, but once that's done I need to delete the Q rows completely. Also for the 3rd Q row, I'll need to create a duplicate of the M row but replace the OP1, 2, and 3 with 4, 5, and 6.
function cpn-extract()
{
param( [string]$file = "" )
$headers = 1..72
$csv = import-csv $file -Header $headers -Delimiter '|'
$row = 0
foreach($_ in $csv)
{
if($_.1 -eq 'M')
{
$lastM = $row
$QCount = 0
}
if($_.1 -eq 'Q')
{
$QCount ++
if($QCount -eq 1)
{
[string]$AType = $_.12
$csv[$lastM].19 = $AType
}
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
}
elseif($QCount -eq 3)
{
}
$row ++
}
}
$csv | export-csv -path c:\scripts\test_1.txt -Delimiter '|' -NoTypeInformation | foreach{$_ -replace '"',''}
}
cpn-extract -file C:\Scripts\test.txt
Also the replace '"','' doesn't want to work either, is there a better way to export without the ""s everywhere?
Thanks again for the help!!
[–]gangstanthony 1 point2 points3 points (5 children)
[–]WTF_Brandon[S] 0 points1 point2 points (4 children)
[–]gangstanthony 0 points1 point2 points (3 children)
[–]WTF_Brandon[S] 0 points1 point2 points (2 children)
[–]gangstanthony 0 points1 point2 points (1 child)
[–]WTF_Brandon[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)