我想在表中的列中获得差异,但我只能与两列进行比较,并且它不会显示所有列。我需要查看第一列中单元格不在哪一列。
文件:
ComputerName OtherComputerName OtherComputer AndAgain infra-1 infra-852 infra-2 infra-99 infra-98 infra-85 infra-44 infra-23 infra-5 infra-8 infra-1 infra-1 infra-2 infra-55 infra-8 infra-70 infra-62 infra-5 infra-852 infra-5
目前的结果:
ComputerName OtherComputerName OtherComputer AndAgain ------------ ----------------- ------------- --------
我想在PowerShell中得到这个结果:
ComputerName OtherComputerName OtherComputer AndAgain ------------ ----------------- ------------- -------- infra-1 infra-852 infra-99 infra-98 infra-85 infra-44 infra-23 infra-5 infra-8 infra-2 infra-55 infra-8 infra-70 infra-62 infra-852
修改
运行此代码时,我也遇到了一些错误。
剧本:
$csv = Import-Csv .\test1.csv -Delimiter ';'
$ref = @($csv.ComputerName)
foreach ($row in $csv) {
foreach ($col in 'OtherComputerName', 'OtherComputer', 'AndAgain') {
if ($ref -contains $row.$col) { $row.$col = '' }
}
}
$table = @()
$file = Get-Content .\test1.csv
$file = $file -replace(" "," ")
$file = $file -replace(" "," ")
$file = $file -replace(" "," ")
$file = $file -replace(" - "," ")
[string[]]$a = Import-Csv '.\test1.csv' | select -Expand ComputerName
[string[]]$b = Import-Csv '.\test1.csv' | select -Expand OtherComputerName
[string[]]$c = Import-Csv '.\test1.csv' | select -Expand OtherComputer
[string[]]$d = Import-Csv '.\test1.csv' | select -Expand AndAgain
$b | ? {$a -notcontains $_}
$c | ? {$a -notcontains $_}
$d | ? {$a -notcontains $_}
foreach ($line in $file) {
$lineb = $line -split(" ")
$obj = New-Object PSObject
$obj | Add-Member -Name "ComputerName" -MemberType NoteProperty -Value $lineb[1]
$obj | Add-Member -Name "OtherComputerName" -MemberType NoteProperty -Value $lineb[1]
$obj | Add-Member -Name "OtherComputer" -MemberType NoteProperty -Value $lineb[2]
$obj | Add-Member -Name "AndAgain" -MemberType NoteProperty -Value $lineb[3]
$table += $obj
}
$table | ft -AutoSize
答案 0 :(得分:0)
您需要将第一列的值读入数组,将其他列的值与该数组进行比较,并清除匹配字段:
$csv = Import-Csv .\test1.csv -Delimiter ';'
$ref = @($csv | Select-Object -Expand ComputerName)
foreach ($row in $csv) {
foreach ($col in 'OtherComputerName', 'OtherComputer', 'AndAgain') {
if ($ref -contains $row.$col) { $row.$col = '' }
}
}