2017-06-16 79 views
1

我想知道爲什麼當我運行我的腳本結果顯示我「;」在我的陣列之間。實際上我試圖比較兩個excel列並顯示差異。如果單元格不在第一列中,而是在第二列中顯示該列。我怎樣才能刪除這些「;」並獲得列之間的區別?我也試着與一列一些文件,它的工作原理PowerShell腳本:結果問題

結果:

ComputerName;;;;;OtherComputerName 
---------------------------------- 
uiojk;;;;;uih      
hjbyu;;;;;ubyhi` 

腳本:

#get the content of the column in the file 
[string[]]$a = import-csv '.\test1.csv' | select -expand ComputerName 
[string[]]$d = import-csv '.\test1.csv' | select -expand OtherComputerName 

#display the cells which aren't in the $a list 

$d |? {$a -notcontains $_} 

#displays the columns of one file 

[Array]$stores[2].ComputerName 
[Array]$stores[2].OtherComputerName 

$stores 

回答

2

Import-Csv使用逗號作爲默認分隔符,如果你想使用另一個字符,那麼你將需要使用-Delimiter選項。類似這樣的:

$x = Import-Csv .\test1.csv -Delimiter ';' 
Compare-Object -ReferenceObject $x.ComputerName -DifferenceObject $x.OtherComputerName 

此代碼會生成警告,因爲並非所有的CSV列都有標題,但它仍然有效。