Created
January 27, 2020 16:26
-
-
Save crossan007/7a1dd45e2a00f15fe9fd38b3ae9b54fa to your computer and use it in GitHub Desktop.
Given a text file containing lines representing "cells" of a data grid, this script converts the lines into a set of objects (where the number of intended columns is set yb $offset)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$lines = Get-content ./t.txt #text file containing "single grid cells" as lines | |
$offset = 11 # desired number of columns in grid | |
$Header = @() | |
$tempObject = New-Object -TypeName PSObject | |
$objects = @() | |
For($i=0;$i -lt $lines.length;$i+=1) { | |
Write-host "Processing line $i of $($lines.length) in chunks of $offset" | |
if ($i -eq $offset) { | |
Write-host "Found Header $($header -join ',')" | |
} | |
if ($i -lt $offset){ | |
$header += $lines[$i] | |
} | |
else { | |
if ($i -gt $offset -and $i % $offset -eq 0) { | |
Write-Host "Starting new object" | |
$objects += $tempObject.PSObject.Copy() | |
$tempObject = New-Object -TypeName PSObject | |
} | |
Write-Host "Adding member to property $($header[$i%$offset]): $($lines[$i])" | |
Add-Member -InputObject $tempObject -Name $($header[$i%$offset]) -Value $($lines[$i]) -MemberType NoteProperty | |
} | |
} | |
$objects |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment