用Compare-Object比较值



从csv文件中获得一堆值,列看起来像这样:

shouston
cgonzalez
bbrown
hlader
kpesavento
jbloom
polson
bcharlow
bcharlow
bkalt

需要查找重复项并修改它们

# Grab CSV file
$inputFile = Import-Csv -Path $filePath
$text = (Get-Culture).TextInfo
$HashSet = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
foreach ($line in $inputFile) {
$name = $line.name
$line.name = $text.ToTitleCase($name)
$firstName = $line.name.split(" ")[0]
$lastName = $line.name.split(" ")[1]
$newEmail = ($firstName[0] + $lastName).toLower()
if (!$HashSet.Add($newEmail)) {
$line.email = ($firstName[0] + $lastName + $line.location_id + "@abc.com").toLower()
}
else {
$line.email = ($firstName[0] + $lastName + "@abc.com").toLower()
}
} 
$inputFile | Export-Csv $fullPath

但是它没有像我预期的那样工作-找到一个重复的值并修改它们。在我的例子中,位置也必须添加到第一个重复的电子邮件中,例如bcharlow1 bcharlow2,而不是bcharlow2。谁能帮助我-我需要如何修改脚本?

您可以使用Group-Object查看重复项,因此执行以下操作:

$val=@(
'**name**',
'shouston',
'cgonzalez',
'bbrown',
'hlader',
'kpesavento',
'jbloom',
'polson',
'bcharlow',
'bcharlow',
'bkalt'
)
$val | Group-Object

将给出给定值被看到的次数。如果你想看前3个你可以通过管道排序然后选择

$val | Group-Object -NoElement | Sort-Object Count -Descending | Select-Object -First 3
Count Name                     
----- ----                     
2 bcharlow                 
1 **name**                 
1 shouston 

可能最简单(也是最快)的方法是使用HashSet:

$InputFile = ConvertFrom-Csv @'
id, location_id, name,                 title,                              email, department
1,           1, "Susan houston",      DS,                                      ,
2,           1, "Christina Gonzalez", D,                                       ,
3,           2, "Brenda brown",       "Director, Second Career Services",      ,
4,           3, "Howard Lader",       "Manager, Senior Counseling",            ,
5,           4, "Kimberly Pesavento", "Commercial director",                   ,
6,           5, "Joe Bloom",          Finance,                                 ,          7
8,           6, "Bart charlow",       Director,                                ,
9,           7, "Bart Charlow",       Director,                                ,
'@
$Emails = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
foreach ($line in $inputFile) {
$name = $line.name
$line.name = (Get-Culture).TextInfo.ToTitleCase($name)
$firstName = $line.name.split(" ")[0]
$lastName = $line.name.split(" ")[1]
$newEmail = ($firstName[0] + $lastName).toLower()
if (!$Emails.Add($newEmail)) {
$line.email = ($firstName[0] + $lastName + $line.location_id + "@abc.com").toLower()
}
else {
$line.email = ($firstName[0] + $lastName + "@abc.com").toLower()
}
}
$InputFile |ConvertTo-Csv
"id","location_id","name","title","email","department"
"1","1","Susan Houston","DS","shouston@abc.com",""
"2","1","Christina Gonzalez","D","cgonzalez@abc.com",""
"3","2","Brenda Brown","Director, Second Career Services","bbrown@abc.com",""
"4","3","Howard Lader","Manager, Senior Counseling","hlader@abc.com",""
"5","4","Kimberly Pesavento","Commercial director","kpesavento@abc.com",""
"6","5","Joe Bloom","Finance","jbloom@abc.com","7"
"8","6","Bart Charlow","Director","bcharlow@abc.com",""
"9","7","Bart Charlow","Director","bcharlow7@abc.com",

相关内容

  • 没有找到相关文章

最新更新