I've got a txt file with the following content:
#test.txt
'ALDHT21;MIMO;1111;BOK;Tree'
'ALDHT21;MIMO;1211;BOK;Tree'
'PRGHT21;AIMO;1351;STE;Water'
'PRGHT21;AIMO;8888;FRA;Stone'
'ABCDT22;DIDO;8888;STE;Stone'
'PRA2HT21;ADDO;8888;STE;Stone'
';ADDO;1317;STE;Stone'
To make it easier to explain, let's give the above content headers: ''Group;Code;ID;Signature;Type'
With the help of Powershell, I'm trying to create a foreach loop of each unique "Signature" to return two variables with unique data from rows where the "Signature" exists in and then mashed together with some delimiters.
Based on the file content, here are the expected results:
First loop:
$Signature = "BOK"
$Groups = "Tree:ALDHT21"
$Codes = "Tree:MIMO"
Next loop:
$Signature = "FRA"
$Groups = "Stone:PRGHT21"
$Codes = "Stone:AIMO"
Last loop:
$Signature = "STE"
$Groups = "Stone:PRA2HT21,Stone:ABCDT22,Water:PRGHT21"
$Codes = "Stone:ADDO,Stone:DIDO,Water:AIMO"
Notice the last loop should skip the last entry in the file because it contains an empty Group.
My attempt didn't quite hit the mark and I'm struggling to find a good way to accomplish this:
$file = "C:\temp\test.txt"
$uniqueSigs = (gc $file) -replace "'$|^'" | ConvertFrom-Csv -Delimiter ';' -Header Group,Code,ID,Signature,Type | group Signature
foreach ($sigs in $uniqueSigs) {
$Groups = ""
foreach ($Group in $sigs.Group) {
$Groups += "$($Group.Type):$($Group.Group),"
}
$Groups = $Groups -replace ",$"
[PSCustomObject] @{
Signatur = $sigs.Name
Groups = $Groups
}
$Codes = ""
foreach ($Group in $sigs.Group) {
$Codes += "$($Group.Type):$($Group.Code),"
}
$Codes = $Codes -replace ",$"
[PSCustomObject] @{
Signatur = $sigs.Name
Codes = $Codes
}
$Signature = $sigs.Name
If ($Group.Group){
write-host "$Signature "-" $Groups "-" $Codes "
}
}
Result from my bad attempt:
BOK - Tree:ALDHT21,Tree:ALDHT21 - Tree:MIMO,Tree:MIMO
FRA - Stone:PRGHT21 - Stone:AIMO
Any help appreciated. :)