【问题标题】:Powershell get first name without second namePowershell得到没有第二个名字的名字
【发布时间】:2020-06-25 16:58:16
【问题描述】:

我有一个写在 Word 文档中的脚本。现在我必须改变性别(我用名字列表来做这件事),问题是这里的很多人都有第二个名字,要么带有“-”,要么中间有空格。

例子

John-Mark smith
Alexander Robert Bails
Sasha baster

我的脚本只有在我能得到第一个名字(John/Alexander/Sasha)的情况下才有效,但我不知道我该怎么做。我从 txt 中获取用户。具有以下标题的文件: ID,firstname,lastname,Account,rndPW,SMTP,...,...

$smtp = $_.smtp
$username = $_.account
$name = $_.lastname
$name1 = $_.lastname
$name2 = $_.lastname
$fistname= $_.firstname
$firstname1= $_.firstname
$passwort = $_.rndPW


$strTitel = "Dear Sir/Madam "+$name
get-content "D:\Files\PowerShell\Work\User\createPDF\test\firstnames_male.csv" | where-object {$_ -eq $firstname[0]} | foreach-object { $strTitel = "Dear Sir"+$name }
get-content "D:\Files\PowerShell\Work\User\createPDF\test\firstnames_female.csv" | where-object {$_ -eq $firstname[0]} | foreach-object { $strTitel = "Dear Madam"+$name }
get-content "D:\Files\PowerShell\Work\User\createPDF\test\firstnames_unspecific.csv" | where-object {$_ -eq $firstname[0]} | foreach-object { $strTitel = "Dear Sir / Madam"+$name }

【问题讨论】:

    标签: string powershell


    【解决方案1】:

    我假设 $name 变量是字符串形式的名称。我们只需要它的第一部分,直到第一个空格或破折号 (-) 字符。

    $nameslist = $name.Replace("-", " ").Split(" ")
    $firstfirstname = $nameslist | Select-Object -first 1
    

    我猜第一个名字决定了性别,不需要考虑所有这些。

    【讨论】:

    • 谢谢你的工作,我只需要稍微切换一下这条车道: where-object {$_ -eq $firstname[0]} -->where-object {$_ -eq $firstfirstname }
    猜你喜欢
    • 2019-06-23
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    相关资源
    最近更新 更多