【发布时间】:2018-03-29 15:42:47
【问题描述】:
我使用此代码匹配两个 CSV 文件并获取我需要的列
在这段代码中,我比较了Matriculename 和Firstname 的数据,当我得到匹配时,我可以检索到“IGG”列
但是很慢……(18行20分钟)
有人可以帮助我吗?
这是我的代码:
foreach ($item in $fileContentIMM)
{
try
{
$Matricule = $item.'Matricule'
$name = $item.'Nom'
$firstname = $item.'Prenom'
# find first matching row in $$fileContentMagic using wildcard
$objMatch = $fileContentMagic | where { $_.'Matricule' -eq $Matricule -and $_.'NOM' -eq $name -and $_.'PRENOM' -eq $firstname}
##### check if any match found
if ($objMatch -eq $null)
{
$item | ForEach-Object {
$filechecktrue += [pscustomobject]@{
'MATRICULE' = $item.'Matricule'
'IGG' = 'noSet'
'NAME' = $item.'Nom'
'FIRSTNAME' = $item.'Prenom'
'SERVICE' = $item.'Service'
'Immeuble'= $item.'Immeuble'
'Niveau' = $item.'Niveau'
'Loc.' = $item.'Loc.'
'PDT' = $item.'PDT'
'Occ.' = $item.'Occ.'
'Site' = $item.'Site'
}
}
}
else
{
$item | ForEach-Object {
$filechecktrue += [pscustomobject]@{
'MATRICULE' = $item.'Matricule'
'IGG' = ($objMatch.'IGG' -join '/')
'NAME' = $item.'Nom'
'FIRSTNAME' = $item.'Prenom'
'SERVICE' = $item.'Service'
'Immeuble'= $item.'Immeuble'
'Niveau' = $item.'Niveau'
'Loc.' = $item.'Loc.'
'PDT' = $item.'PDT'
'Occ.' = $item.'Occ.'
'Site' = $item.'Site'
}
}
}
}
catch
{
"ERROR: Problem reading line - skipping :" | Out-File $LogFile -Append -Force
$item.nom + $item.prenom + $item.service| Out-File $LogFile -Append -Force
}
}
【问题讨论】:
-
20分钟找出18行有多少?你看过
Compare-Object吗? -
你确定这是慢的部分吗?两个 csv 文件有多大?您是否测量或使用过 ex。
Write-Host "import done"确定不是读取慢的文件? -
文件 contentIMM 包含 18 行和 filecontentMagic 45000
-
也许我要去看看谢谢
标签: performance powershell csv