读取 txt 文件,将行更改为列,保存 txt 文件

Reading txt-file, change rows to columns, save txt file

我有一个包含超过 300 万条记录的 txt 文件(以分号分隔),其中第 1 至 4 列包含一些一般信息。第 5 列和第 6 列有详细信息。相同的一般信息在第 1 列到第 4 列最多可以有 4 个不同的详细信息。

我的示例输入:

Server;Owner;Company;Username;Property;Value
Srv1;Dave;Sandbox;kwus91;Memory;4GB
Srv1;Dave;Sandbox;kwus91;Processes;135
Srv1;Dave;Sandbox;kwus91;Storage;120GB
Srv1;Dave;Sandbox;kwus91;Variant;16
Srv2;Pete;GWZ;aiwq71;Memory;8GB
Srv2;Pete;GWZ;aiwq71;Processes;234
Srv3;Micael;P12;mxuq01;Memory;16GB
Srv3;Micael;P12;mxuq01;Processes;239
Srv3;Micael;P12;mxuq01;Storage;160GB
Srv4;Stefan;MTC;spq61ep;Storage;120GB

期望的输出:

Server;Owner;Company;Username;Memory;Processes;Storage;Variant
Srv1;Dave;Sandbox;kwus91;4GB;135;120GB;16
Srv2;Pete;GWZ;aiwq71;8GB;234;;
Srv3;Micael;P12;mxuq01;16GB;239;160GB;
Srv4;Stefan;MTC;spq61ep;;;120GB;

如果一般信息(第 1-4 列)不存在值,则必须留空。

我当前的代码:

$a = Import-csv .\Input.txt -Delimiter ";"
$a | FT -AutoSize

$b = @()
foreach ($Server in $a.Server | Select -Unique) {
    $Props = [ordered]@{ Server = $Server }
    $Owner = ($a.where({ $_.Server -eq $Server})).Owner | Select -Unique
    $Company = ($a.where({ $_.Server -eq $Server})).Company | Select -Unique
    $Username = ($a.where({ $_.Server -eq $Server})).Username | Select -Unique

    $Props += @{Owner = $Owner}
    $Props += @{Company = $Company}
    $Props += @{Username = $Username}
    foreach ($Property in $a.Property | Select -Unique){ 
        $Value = ($a.where({ $_.Server -eq $Server -and 
                    $_.Property -eq $Property})).Value

        $Props += @{ $Property = $Value }
    }
    $b += New-Object -TypeName PSObject -Property $Props
}
$b | FT -AutoSize
$b | Export-Csv .\Output.txt -NoTypeInformation -Delimiter ";"

经过大量尝试并出现错误:我的脚本有效。
但这需要很多时间。
是否有可能使 txt 文件中大约 300 万行的性能更好?我正在计算 $Server.

的大约 250 万个唯一值

我是 运行 Windows 7 64 位 PowerShell 4.0。

尝试这样的事情:

#Import Data and create empty columns
$List=import-csv "C:\temp\file.csv" -Delimiter ";"

#get all properties name with value not empty    
$ListProperty=($List | where Value -ne '' | select property -Unique).Property

#group by server
$Groups=$List | group Server

#loop every rows and store data by group and Property Name
$List | %{

    $Current=$_

    #Take value not empty and group by Property Name
    $Group=($Groups | where Name -eq $Current.Server).Group | where Value -ne '' |  group Property

   #Add all property and first value not empty
    $ListProperty | %{
        $PropertyName=$_
        $PropertyValue=($Group | where Name -eq $PropertyName | select -first 1).Group.Value
        $Current | Add-Member -Name $PropertyName -MemberType NoteProperty -Value $PropertyValue
    }

$Current

} | select * -ExcludeProperty Property, Value -unique  | export-csv "c:\temp\result.csv" -notype -Delimiter ";"