gpt4 book ai didi

powershell - 检查一个大型的csv文件以匹配ID花费很长时间

转载 作者:行者123 更新时间:2023-12-02 23:35:42 25 4
gpt4 key购买 nike

下面的Powershell正常运行-但是在50k的csv上花费的时间很长,例如10分钟。我敢肯定,我会使其工作比需要的更加努力-我只需要匹配ID列,然后为每次匹配返回列。关于如何使此过程更快更高效的任何想法?

$ID = @()
$fname = @()
$lname = @()
$mname = @()
$streetadd = @()
$apartment = @()
$city = @()
$state = @()
$zip = @()


$Ids = @(0317,11432,1104,9999,1955)

Write-Host "PLEASE WAIT >>> "


Import-Csv C:\mycsv.csv |`
ForEach-Object {
$ID += $_."ID"
$fname += $_."First Name"
$lname += $_."Last Name"
$mname += $_."Middle Name"
$streetadd += $_."Street Address"
$apartment += $_."Apartment"
$city += $_."City"
$state += $_."State"
$zip += $_."Zip"

}

foreach ($Id in $Ids) {
foreach ($elem in $ID) {
# Write-Host $Id
if ($Id -contains $elem)
{
#Write-Host "Customer Exists!"
$Where = [array]::IndexOf($ID, $elem)
Write-Host $ID[$Where] $fName[$Where] $lname[$Where] $mname[$where] $streetadd[$where] $apartment[$where] $city[$where] $state[$where] $zip[$where]
}
}

}


尝试从@Moerwald下面的答案中获取以下代码,但未获得任何结果-

$Ids = @(1317,1132,110,9999,1955)
$rows = @(Import-Csv C:\mycsv-csv.csv |? { $Ids -contains $_.id})

foreach ($r in $rows) {
write-host $r.id; $r.fname
}

最佳答案

$Ids = @(0317,11432,1104,9999,1955)
$rows = @(Import-Csv C:\mycsv.csv |? { $Ids -contains $_.ID})
$rows将是已过滤行的数组。您可以通过以下方法遍历数组:
$rows | % { Write-Host "$($_.Id)"} 
$_引用已过滤的行,并具有与列名相对应的属性。
?where-object cmdlet的快捷方式。
%foreach-object cmdlet的快捷方式。

更新:

此代码有效:
$s =@'
Student ID,OtherID,First Name,Last Name,Middle Name,Birth Date,,,,,,,,Street Address Line 1,Street Address Line 2,Apartment,City,State,Zip
1317,,a,b,c,6/11/2019,,,,,,,,1 5th dr,,,main,nv,55555
1132,,d,e,f,6/10/2019,,,,,,,,7 24th dr,,,duke,az,55555
'@

$csv = convertfrom-csv $s
$Ids = @(1317,1132, 11432,1104,9999,1955)

$rows = $csv |? { $Ids -contains $_.'Student ID'}

$rows | % { $_.'Student ID'}

返回:
1317
1132

这是正在运行的版本的 link

关于powershell - 检查一个大型的csv文件以匹配ID花费很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56529385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com