[英]How to use Group-Object on this?
I am trying to get all the accounts from $f
which do not match the accounts in $table4
into $accounts
. 我正在尝试将与$table4
中的帐户不匹配的$f
所有帐户转换为$accounts
。 But I need to also check if the occupancy number matches or not. 但是我还需要检查入住人数是否匹配。
CSV $f
: CSV $f
:
Account_no |occupant_code -----------|------------ 12345 | 1 67890 | 2 45678 | 3
DataTable $table4
数据表$table4
Account_no |occupant_code -----------|------------ 12345 | 1 67890 | 1 45678 | 3
Current code: 当前代码:
$accounts = Import-Csv $f |
select account_no, occupant_code |
where { $table4.account_no -notcontains $_.account_no }
What this needs to do is to check that occupant_code
doesn't match, ie: 这需要做的是检查occupant_code
不匹配,即:
$f
and $table4
match; 12345 : $f
和$table4
中的帐户和占用者匹配; so it's ignored 所以被忽略了 $table4
, but occupancy_code does not match, so it is added to $accounts
. 67890 :帐户与$table4
匹配,但是occupancy_code不匹配,因此将其添加到$accounts
。 Current result: 当前结果:
Desired result: 67890
预期结果: 67890
I believe I need to use Group-Object
, but I do not know how to use that correctly. 我相信我需要使用Group-Object
,但是我不知道如何正确使用它。
I tried: 我试过了:
Import-Csv $f |
select account_no, occupant_code |
Group-Object account_no |
Where-Object { $_.Group.occupant_code -notcontains $table4.occupant_code }
Compare-Object
? Compare-Object
?
csv1.csv: csv1.csv:
Account_no,occupant_code
12345,1
67890,2
45678,3
csv2.csv: csv2.csv:
Account_no,occupant_code
12345,1
67890,1
45678,3
PowerShell command: PowerShell命令:
Compare-Object (Import-Csv .\csv1.csv) (Import-Csv .\csv2.csv) -Property occupant_code -PassThru
Output: 输出:
Account_no occupant_code SideIndicator
---------- ------------- -------------
67890 1 =>
67890 2 <=
An alternative to Bill's suggestion would be to fill a hashtable with your reference data ( $table4
) and look up the occupant_code
value for each account from $f
, assuming that your account numbers are unique: 比尔建议的另一种选择是用您的参考数据( $table4
)填充哈希表 ,并从$f
查找每个帐户的occupant_code
值,假设您的帐号是唯一的:
$ref = @{}
$table4 | ForEach-Object {
$ref[$_.Account_no] = $_.occupant_code
}
$accounts = Import-Csv $f |
Where-Object { $_.occupant_code -ne $ref[$_.Account_no] } |
Select-Object -Expand Account_no
$f | InnerJoin $table4 {$Left.Account_no -eq $Right.Account_no -and $Left.occupant_code -ne $Right.occupant_code} @{Account_no = {$Left.$_}} | Format-Table
Result: 结果:
occupant_code Account_no
------------- ----------
{2, 1} 67890
For details see: In Powershell, what's the best way to join two tables into one? 有关详细信息,请参见: 在Powershell中,将两个表合并为一个的最佳方法是什么?
In addition to all the other answers, you might be able to leverage the IndexOf()
method on arrays 除了所有其他答案之外,您还可以在数组上利用IndexOf()
方法
$services = get-service
$services.name.IndexOf("xbgm")
240
I am on a tablet right now and don't have a handy way to test it, but something along these lines might work for you: 我目前在平板电脑上,没有方便的测试方法,但是符合以下要求的方法可能对您有用:
$table4.account_no.IndexOf($_.account_no)
should fetch the index your account_no lives in for $table 4, so you could jam it all into one ugly pipe: 应该为$ 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) }
An inner join or a normal loop might just be cleaner though, especially if you want to add some other stuff in. Since someone posted an innerjoin, you could try a loop like: 但是,内部联接或普通循环可能更干净,特别是如果要添加其他内容。由于有人发布了内部联接,因此可以尝试如下循环:
$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)
}
Edit for OP, he mentioned $table4 is a data.table There is probably a much better way to do this, as I haven't used data.table before, but this seems to work fine: 对于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.