trying to stack these columns in powershell, and it works! however, it feels like there should be an easier way to do this. please let me know if you have alternatives that accomplish the same goal
$lines = @'
5
b d7 e
c f
'@
$lines = $lines.Split("`n")
$max = $lines | % {$_.trim().split(' ').count} | sort -desc | select -f 1
$count = 0
$obj = New-Object psobject
foreach ($line in $lines) {
$obj | Add-Member -MemberType NoteProperty -Name $count -Value $line
$count++
}
for ($x = 0; $x -lt $count; $x++) {
for ($y = 0; $y -lt $count; $y++) {
$obj.$y.trim().split(' ')[$x]
}
}
desired output would be this:
5
b
c
d7
f
e
here's a more random example of input and desired output. the script:
$alpha = 65..90 | % { [char]$_ }
$lines = for ($i = 0; $i -lt $alpha.Count; $i ++) {
$line = ''
$cols = Get-Random -Minimum 1 -Maximum 6
for ($j = 0; $j -lt $cols; $j++) {
$line += $alpha[$i+$j] + ' '
}
$line.Trim()
$i = $i + $cols - 1
}
$lines = $lines.Split("`n")
$lines
# Rest of code same as above.
the first section shows the array. the second section shows what i want it to look like. (reminder: script already works, just looking for alternatives)
A
B C D E
F G
H I
J
K L M N
O
P Q R S
T U V W
X Y Z
A
B
F
H
J
K
O
P
T
X
C
G
I
L
Q
U
Y
D
M
R
V
Z
E
N
S
W