[英]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.