Quickest way to organize categorized data in a text file and convert to CSV

前端 未结 5 1073
粉色の甜心
粉色の甜心 2021-01-24 03:25

I have a text file with hundreds of rows. Data fields and values separated by a colon and one empty line separating each data set. It looks something like this...

ico         


        
5条回答
  •  不知归路
    2021-01-24 04:24

    Try this:

    $CurrentElement=[pscustomobject]@{}
    
    #get all rows and add element list when row empty is founded
    Get-Content "c:\temp\test.txt" | %{
    
        if ($_ -eq "")
        {
            $CurrentElement
            $CurrentElement=[pscustomobject]@{}
        }
        else
        {
           $Row=$_.split(':')
           Add-Member -InputObject $CurrentElement -MemberType NoteProperty -Name $Row[0] -Value $Row[1]
        }
    
    }  | export-csv "c:\temp\result.csv" -notype
    
    $CurrentElement  | export-csv "c:\temp\result.csv" -notype -Append
    

提交回复
热议问题