I have a text file with hundreds of rows. Data fields and values separated by a colon and one empty line separating each data set. It looks something like this...
ico
here's another way to do the job with a combo of simple regex patterns and string operators.
$InStuff = @'
column1:value1
column2:value2
column3:value3
column4:value4
column5:value5
column1:value6
column2:value7
column3:value8
column4:value9
column5:value10
column1:value11
column2:value12
column3:value13
column4:value14
column5:value15
'@
$SplitInStuff = $InStuff -split ([environment]::NewLine * 2)
$HeaderLine = ($SplitInStuff[0] -replace '(?m):.+$').Split([environment]::NewLine) -join ', '
$CSV_Text = [System.Collections.Generic.List[string]]::new()
$CSV_Text.Add($HeaderLine)
foreach ($SIS_Item in $SplitInStuff)
{
$CSV_Text.Add(($SIS_Item -replace '(?m)^.+:').Split([environment]::NewLine).Where({$_}) -join ', ')
}
$Results = $CSV_Text |
ConvertFrom-Csv
# on screen
$Results |
Format-Table
# to CSV
$Results |
Export-Csv -LiteralPath "$env:TEMP\JohnnyCarino_ReformatedData.csv" -NoTypeInformation
output ...
column1 column2 column3 column4 column5
------- ------- ------- ------- -------
value1 value2 value3 value4 value5
value6 value7 value8 value9 value10
value11 value12 value13 value14 value15
CSV file content ...
"column1","column2","column3","column4","column5"
"value1","value2","value3","value4","value5"
"value6","value7","value8 ","value9","value10"
"value11 ","value12","value13 ","value14","value15"