Skip to content

Instantly share code, notes, and snippets.

@crossan007
Created January 27, 2020 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crossan007/7a1dd45e2a00f15fe9fd38b3ae9b54fa to your computer and use it in GitHub Desktop.
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)
$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