powershell - Converting a text file with one column to multiple column -


i have file data in 1 column format. need use file input file , output file should in multi column format. need script conversion. not matter powershell or batch.

input file content:input.txt

store1:
apple
orange
peach
the end

store2:
tree
park
pond
bird
the end

store3:
building
road
peach
store
grocery
the end

the output file should be:
store1:,store2:,store3:
apple, tree, building
orange, park, road
peach, pond, peach
, bird, store
, , grocery

i know gimmie, took learning opportunity myself, , since have code maybe else can learn it

$text = gc c:\temp\input.txt $groups = ($text | out-string) -split 'the end' | ? {$_ -notmatch '^(?:\s+)?$'} $columns = $groups | % {$_.trim().split("`n")[0]} $rows = $groups | % {$_.trim().split("`n").count - 2} | sort -desc | select -f 1  $result = 0..$rows | % {     $row = $_     $obj = new-object psobject     0..$($columns.count-1) | % {         $column = $columns[$_]         $store = $groups[$_].trim().split("`n")         $item = $store[$row+1]         $obj | add-member -membertype noteproperty -name $column.trim() -value $(if ($item) {$item.trim()})     }     $obj }  $result | epcsv c:\temp\input.csv -notypeinformation 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -