-1

How would columns of data for a block of text:

nicholas@mordor:~/powershell$ 
nicholas@mordor:~/powershell$ cat multiple_lines.data 
a 4
b 5
d 6
e 7
nicholas@mordor:~/powershell$ 
nicholas@mordor:~/powershell$ datamash transpose < multiple_lines.data > transposed.data
nicholas@mordor:~/powershell$ 
nicholas@mordor:~/powershell$ cat transposed.data 
a 4 b 5 d 6 e 7
nicholas@mordor:~/powershell$ 
nicholas@mordor:~/powershell$ datamash transpose < transposed.data 
a 4
b 5
d 6
e 7
nicholas@mordor:~/powershell$ 

be fed into a CSV type file so that column a has the value 4, etc? Here c has been omitted, but it can be assumed to be present. Or, at least that missing columns can be added.

No doubt awk would be fantastic at grabbing the above numbers, but looking to use PowerShell here. Output to json or xml would be just as good as CSV, most any sort of output like data interchange format would be fine.

Assuming an array of such blocks.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Isn't that already answered [here](https://stackoverflow.com/a/75221265/1701026)? – iRon Jan 30 '23 at 10:11
  • I was more thinking of iterating a collection of such matrices @iRon – Nicholas Saunders Jan 31 '23 at 22:09
  • 1
    Again, isn't that what the referred answer does? Please try to build an PowerShell [mcve] that shows how you create your source collection and how the destination collection should be created. Displaying the output in the cli simply doesn't reveal enough information to help which where you looking for. – iRon Feb 01 '23 at 07:08

1 Answers1

-1

Use Import-Csv instead of ConvertFrom-Csv when reading from a file. Powershell will recognize automaically the space deliminater instead of the comma.

$txt = @"
a 4
b 5
d 6
e 7
"@
$table = $txt | ConvertFrom-Csv
$table
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • No, [`ConvertFrom-Csv`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/convertfrom-csv) recognizes only `,` as the field separator (delimiter) by default; you can override it with `-Delimiter`. That alone wouldn't be enough here, however, given that a _header row_ is missing. You can easily verify that your code doesn't do what you think it does with `$table | Format-List` – mklement0 Feb 05 '23 at 21:29