PowerShell的转置和保留只有一套头
我有一个如下所示的数据集
headers data ip 192.168.1.1 netmask 255.255.255.0 description class C ip 172.20.1.1 netmask 255.255.0.0 description class B ip 10.0.0.1 netmask 255.255.255.0 description class A
我怎样才能把它变成我的预期输出:
ip netmask description 192.168.1.1 255.255.255.0 class C 172.20.1.1 255.255.0.0 class B 10.0.0.1 255.0.0.0 class A
目前我从CSV导入这个数据如下:
$data = import-csv .\data.txt -headers headers,data
如果在Excel中有一个简单的方法来做到这一点,那也是非常棒的。
任何援助将是伟大的。 谢谢。
以前的答案没有使用问题中指定的数据,这是一个问题。 它也完全评论说明它做了什么。 下面的代码是如何工作的简要说明:
$newrow = @{} # newrow contains the values on an output row at any given time $newtable = [System.Collections.ArrayList]@() # newtable contains the final output # Loop through each row in the input data, assign oldrow to the current row foreach($oldrow in $data) { # If the output row already has an ip, netmask or description, then start a new row (hashtable) and add the current row (hashtable) to output if ($newrow[$oldrow.headers] -ne $null) { Write-Host "$($oldrow.headers) Already found. Adding new row to output" -ForegroundColor Green # Add the current row to the target object [void]$newtable.Add($(New-Object PSObject -Property $newrow)) # Create a new empty row (hashtable) $newrow = @{} } # For each iteration, keep adding data Write-Host "Adding new entry for $($oldrow.headers)" $newrow[$oldrow.headers] = $oldrow.data } # The final row is not added to the table within the loop, so it must be added here Write-Host "Loop finished, adding final row to output" -ForegroundColor Green [void]$newtable.Add($(New-Object PSObject -Property $newrow)) $newtable | select ip,netmask,description
假设
遇到具有相同标题值的logging时,需要新行。 在此之前,应该将logging添加到同一行,并且标题值提供新的列名称。
处理
- 设置一个名为$ newrow的哈希表。
- 设置一个名为$ newtable的ArrayList。 ArrayLists比标准数组更灵活,因为它们有一个
Add()
方法。 - 使用foreach循环遍历数据
- 从if语句开始,该语句处理用已经分配的列来查找logging。 此时使用
Write-Host
发出消息,添加旧行并设置新行。 在foreach循环的第一次迭代中,由于散列表为空,所以总是计算为false。- 在
if
条件之外的块中,向hashtable添加值。 - 最后,添加最后一行,然后使用select-object以正确的顺序打印输出。
- 在
#import数据
$data = Import-Csv .\data.txt -headers headers,data $data | FT -AutoSize
#删除第一行
get-content $data | select -Skip 1 | set-content "$data-temp" move "$data-temp" $data -Force
#转换表格
$Duration = Measure-Command { $b = @() foreach ($Property in $data.Property | Select -Unique) { $Props = [ordered]@{ Property = $Property } foreach ($Server in $data.Server | Select -Unique){ $Value = ($data.where({ $_.Server -eq $Server -and $_.Property -eq $Property })).Value $Props += @{ $Server = $Value } } $b += New-Object -TypeName PSObject -Property $Props } } Write-Host "Finished transposing " -ForegroundColor Green -NoNewline Write-Host "$(($data | Get-Member -MemberType Properties).count)/$($data.Count)" -ForegroundColor Yellow -NoNewline Write-Host " columns/rows into " -ForegroundColor Green -NoNewline Write-Host "$(($b | Get-Member -MemberType Properties).count)/$($b.Count)" -ForegroundColor Yellow -NoNewline Write-Host " columns/rows in " -ForegroundColor Green -NoNewline Write-Host $Duration.Milliseconds -ForegroundColor Yellow -NoNewline Write-Host " Milliseconds" -ForegroundColor Green $b | FT -AutoSize $b | Out-GridView $b | Export-Csv .\newData.csv -NoTypeInformation
newData.csv应该看起来像你所期望的,如果没有,让我再次知道。
- 使用Microsoft.ACE.OLEDB.12.0将数据从Excel导入到SQL Server数据库
- 在vba中添加范围
- 使用InStr来确定特定字符的出现次数?
- Excel 2013 VBA使用连续的列对大范围进行sorting
- SpecialCells在Excel 2010中导致SheetSelectionChange事件
- NoClassDefFoundError:UnsupportedFileFormatException当使用Apache poi写入Excel文件
- 加权中值 – 数组的UDF?
- Excel – 从范围search,而不是特定的单元格?
- 在java版本1.4中创build一个excel工作簿