[英]How to use Group-Object on this?
我正在嘗試將與$table4
中的帳戶不匹配的$f
所有帳戶轉換為$accounts
。 但是我還需要檢查入住人數是否匹配。
CSV $f
:
Account_no |occupant_code -----------|------------ 12345 | 1 67890 | 2 45678 | 3
數據表$table4
Account_no |occupant_code -----------|------------ 12345 | 1 67890 | 1 45678 | 3
當前代碼:
$accounts = Import-Csv $f |
select account_no, occupant_code |
where { $table4.account_no -notcontains $_.account_no }
這需要做的是檢查occupant_code
不匹配,即:
$f
和$table4
中的帳戶和占用者匹配; 所以被忽略了 $table4
匹配,但是occupancy_code不匹配,因此將其添加到$accounts
。 當前結果:
預期結果: 67890
我相信我需要使用Group-Object
,但是我不知道如何正確使用它。
我試過了:
Import-Csv $f |
select account_no, occupant_code |
Group-Object account_no |
Where-Object { $_.Group.occupant_code -notcontains $table4.occupant_code }
Compare-Object
?
csv1.csv:
Account_no,occupant_code
12345,1
67890,2
45678,3
csv2.csv:
Account_no,occupant_code
12345,1
67890,1
45678,3
PowerShell命令:
Compare-Object (Import-Csv .\csv1.csv) (Import-Csv .\csv2.csv) -Property occupant_code -PassThru
輸出:
Account_no occupant_code SideIndicator
---------- ------------- -------------
67890 1 =>
67890 2 <=
$f | InnerJoin $table4 {$Left.Account_no -eq $Right.Account_no -and $Left.occupant_code -ne $Right.occupant_code} @{Account_no = {$Left.$_}} | Format-Table
結果:
occupant_code Account_no
------------- ----------
{2, 1} 67890
有關詳細信息,請參見: 在Powershell中,將兩個表合並為一個的最佳方法是什么?
除了所有其他答案之外,您還可以在數組上利用IndexOf()
方法
$services = get-service
$services.name.IndexOf("xbgm")
240
我目前在平板電腦上,沒有方便的測試方法,但是符合以下要求的方法可能對您有用:
$table4.account_no.IndexOf($_.account_no)
應該為$ table 4獲取您account_no所居住的索引,因此您可以將它們全部塞入一個丑陋的管道中:
$accounts = Import-Csv $f | select account_no, occupant_code |
where { ($table4.account_no -notcontains $_.account_no) -or ($table4[$table4.account_no.IndexOf($_.account_no)].occupant_code -ne $_.occupant_code) }
但是,內部聯接或普通循環可能更干凈,特別是如果要添加其他內容。由於有人發布了內部聯接,因此可以嘗試如下循環:
$accounts = new-object System.Collections.ArrayList
$testSet = $table4.account_no
foreach($myThing in Import-Csv $f)
{
if($myThing.account_no -in $testSet )
{
$i = $testSet.IndexOf($myThing.account_no)
if($table4[$i].occupant_code -eq $myThing.occupant_code) {continue}
}
$accounts.add($myThing)
}
對於OP進行編輯,他提到$ table4是data.table,這可能是一種更好的方法,因為我以前沒有使用過data.table,但這似乎可以正常工作:
$table = New-Object system.Data.DataTable
$col1 = New-Object system.Data.DataColumn Account_no,([string])
$col2 = New-Object system.Data.DataColumn occupant_code,([int])
$table.columns.add($col1)
$table.columns.add($col2)
$row = $table.NewRow()
$row.Account_no = "12345"
$row.occupant_code = 1
$table.Rows.Add($row)
$row = $table.NewRow()
$row.Account_no = "67890"
$row.occupant_code = 1
$table.Rows.Add($row)
$row = $table.NewRow()
$row.Account_no = "45678"
$row.occupant_code = 3
$table.Rows.Add($row)
$testList = @()
$testlist += [pscustomobject]@{Account_no = "12345"; occupant_code = 1}
$testlist += [pscustomobject]@{Account_no = "67890"; occupant_code = 2}
$testlist += [pscustomobject]@{Account_no = "45678"; occupant_code = 3}
$accounts = new-object System.Collections.ArrayList
$testSet = $table.account_no
foreach($myThing in $testList)
{
if($myThing.account_no -in $testSet )
{
$i = $testSet.IndexOf($myThing.account_no)
if($table.Rows[$i].occupant_code -eq $myThing.occupant_code) {continue}
}
$accounts.add($myThing) | out-null
}
$accounts
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.