0

To accomplish with powershell:

Original Transposed
0 1 2 3
a b c d
# $ @ %

0 a #
1 b $
2 c @
3 d %

how can a magic number be used (some regex in practice) where each original row has a variable number of columns so that only when the keyword occurs does that initiate a transpose?

Assuming that the original matrix is nx2 so something like:

a 1
b 2
c 3
d 4
a 5
d 6
a 7
b 8
c 9

The resulting matrix may very well be sparse, but each occurrence of a would signify a new column of output.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

1

To transpose Rows to column conversion of file with powershell:
(Note that there is nothing in the referral that shows how magic numbers should be used to accomplish this)

$Orginal = @'
0 1 2 3
a b c d
# $ @ %
'@

$Transposed = [Collections.ObjectModel.Collection[Object]]::new()
$Lines = $Orginal -Split '\r?\n'
for ($y = 0; $y -lt $Lines.Count; $y++) {
    $Items = $lines[$y] -Split '\s+'
    for ($x = 0; $x -lt $Items.Count; $x++) {
        if ($x -ge $Transposed.Count) { $Transposed.Add((,@() * $Lines.Count)) }
        $Transposed[$x][$y] = $Items[$x]
    }
}

$Transposed |Foreach-Object { "$_" }

0 a #
1 b $
2 c @
3 d %
iRon
  • 20,463
  • 10
  • 53
  • 79