【问题标题】:Power Shell Find Duplicate Values and modify themPowershell 查找重复值并修改它们
【发布时间】:2023-02-23 04:18:34
【问题描述】:

有一个解析 csv 文件的脚本,但它不能按需要工作 这是 csv 文件中的一些内容

id,location_id,name,title,email,department
1,1,Susan houston,Director of Services,,
2,1,Christina Gonzalez,Director,,
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,Financial Empowerment Programs Program Director,,
7,6,peter Olson,Director,,
8,6,Bart charlow,Executive Director,,
9,7,Bart Charlow,Executive Director,,
Param 
( 
    [Parameter(Mandatory = $true, Position = 0)] 
    [string]$filePath 
) 
$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 -UseQuotes AsNeeded ./accounts_new.csv

这是脚本的输出

"id","location_id","name","title","email","department"
"1","1","Susan Houston","Director of Services","shouston@abc.com",""
"2","1","Christina Gonzalez","Director","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","Financial Empowerment Programs Program Director","jbloom@abc.com",""
"7","6","Peter Olson","Director","polson@abc.com",""
"8","6","Bart Charlow","Executive Director","bcharlow@abc.com",""
"9","7","Bart Charlow","Executive Director","bcharlow7@abc.com",""

正如您从最后两行中看到的那样,location_id 仅附加了第二封相同的电子邮件,但第一封保持不变,我如何将 location_id 添加到第一封邮件?

你能帮助我吗?谢谢!

我尝试使用 while 而不是 if 语句,但它对我没有帮助

【问题讨论】:

  • 真正的问题是:你为什么要编程任何事物首先解析 CSV 文件?
  • 这是家庭作业,谢谢!
  • 不确定这是否是正确的站点,这需要更多帮助,而不仅仅是指出一个错误。你没有一个可以帮助你的助手吗?目前,您甚至没有跳过第一行或在逗号处拆分。我也没有看到你使用函数,这在开始编程时非常重要。
  • 我不太清楚预期的输出是你的样本数据 - 你能展示它以及当前输出吗?
  • 我同意 mclayton,不清楚 id 8 上的人应该是 bcharlow6@abc.com 还是 bcharlow7@abc.com

标签: powershell csv for-loop parsing


【解决方案1】:

我对此的看法是按 name 属性对 CSV 进行分组,然后如果有 2 个或更多对象具有相同的 name 值,则使用 location_id。为此,您可以使用Group-Object

Param(
    [Parameter(Mandatory = $true, Position = 0)]
    [string] $FilePath
)

function ConvertTo-Email {
    param(
        [Parameter(Mandatory)]
        [string] $Name,

        [Parameter()]
        [string] $Location,

        [Parameter()]
        [string] $Domain = "@abc.com"
    )

    end {
        $newEmail = $Name.ToLower() -split 's+'
        $newEmail[0][0] + $newEmail[1] + $Location + $Domain
    }
}

$txt = (Get-Culture).TextInfo
$csv = Import-Csv -Path $filePath
$map = $csv | Group-Object Name -AsHashTable -AsString

$csv | ForEach-Object {
    $_.name = $txt.ToTitleCase($_.name)

    # if there is only one user with the same name
    if($map[$_.name].Count -eq 1) {
        # use their Name and Last Name only
        $_.email = ConvertTo-Email $_.name
        return $_
    }

    # else, use also their Location_Id
    $_.email = ConvertTo-Email $_.name -Location $_.location_id
    $_
} | Export-Csv -UseQuotes AsNeeded ./accounts_new.csv

在问题中使用示例 Csv 的输出将变为:

id location_id name               title                                           email              department
-- ----------- ----               -----                                           -----              ----------
1  1           Susan Houston      Director of Services                            shouston@abc.com
2  1           Christina Gonzalez Director                                        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          Financial Empowerment Programs Program Director jbloom@abc.com
7  6           Peter Olson        Director                                        polson@abc.com
8  6           Bart Charlow       Executive Director                              bcharlow6@abc.com
9  7           Bart Charlow       Executive Director                              bcharlow7@abc.com

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 2019-03-17
    • 2021-01-02
    • 2021-01-10
    • 2014-12-27
    相关资源
    最近更新 更多