【问题标题】:Replacing values in csv using powershell使用powershell替换csv中的值
【发布时间】:2018-08-13 01:07:23
【问题描述】:

我需要用 email.csv 中的电子邮件地址覆盖 userinfo.csv 中的电子邮件值 userid 和 u_user 值在两个 csv 中都匹配且唯一。 userinfo.csv 中的 email 地址值不好,需要用 email.csv 中的 email 值覆盖。

如何匹配 csv 和附加电子邮件值中的用户 ID?

甚至不知道从哪里开始。请帮忙。

email.csv
userid,email
1234,user4@email.com
1235,user5@email.com

userinfo.csv
u_work,u_user,u_address,u_city,u_state,u_zip,u_email,u_phonehome
1234,here,there,everywhere,55555,1234@bad.org,555-555-5555
away,1235,there,here,everywhere,66666,1235@bad.com,666-666-6666

new.csv
u_work,u_user,u_address,u_city,u_state,u_zip,u_email,u_phonehome
1234,here,there,everywhere,55555,user4@email.com,555-555-5555
away,1235,there,here,everywhere,66666,user5@email.com,666-666-6666

【问题讨论】:

  • 请根据@Bacon Bits 评论/确认/更新您问题中的userinfo.csv:“您提供的 CSV 无效”。
  • 您可以使用来自PowerShell Gallery[Join-Object] cmdletImport-CSV .\userinfo.csv | LeftJoin (Import-CSV .\email.csv) userid {$Right.$_} | Export-CSV .\New.csv

标签: powershell


【解决方案1】:

您提供的 CSV 文件无效。标题行有 8 个字段。第 1 行有 7 个字段。那是无效的。我假设它应该是这样的:

userinfo.csv
u_work,u_user,u_address,u_city,u_state,u_zip,u_email,u_phone
home,1234,here,there,everywhere,55555,1234@bad.org,555-555-5555
away,1235,there,here,everywhere,66666,1235@bad.com,666-666-6666

换句话说,u_phonehome 实际上是 u_phonehome 在您的示例中位于错误的行。

您的基本步骤是:

A.将email.csv 导入哈希表以便快速查找。

$emails = @{}
Import-Csv email.csv | ForEach-Object {
    $email[$_.userid] = $_.email
}

B.导入userinfo.csv,并在必要时替换电子邮件地址。

$NewCSV = Import-Csv userinfo.csv | ForEach-Object {
    if ($emails.ContainsKey($_.u_user)) {
        $_.u_email = $emails[$_.u_user]
    }
    $_
}

C.编写输出文件。

$NewCSV | Export-Csv new.csv -NoTypeInformation

您也可以使用Select-Object 和计算属性执行步骤 B,但这更容易编写。

【讨论】:

  • 感谢您的基本步骤。我肯定错过了什么。我收到一个错误:
  • 无法索引到 System.String 类型的对象
  • @SteveGlover 你在运行什么会产生错误?哪个命令抛出错误?完整的错误信息是什么?
  • 无法索引到空数组。在 C:\TestScripts\New Text Document2.ps1:3 char:5 + $email[$_.Student_id] = $_.Student_email + ~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NullArray
  • @SteveGlover 你有一个基本的逻辑错误。 $email 未初始化为哈希表,或者您将$_ 管道与非管道foreach 语句混合在一起(例如,foreach ($Student in $CSV) { $_.Student_id } 无效),或者Student_idStudent_email不是 CSV 文件中标题的正确拼写。
【解决方案2】:

您可以使用正则表达式进行匹配,并使用替换来修改特定的字符串。这是一件很常见的事情,并且有很多关于这个主题的文章和帖子。因此,请试一试以下资源,如果您需要进一步的帮助,请继续努力。

例如:

Windows PowerShell:编写正则表达式

https://technet.microsoft.com/en-us/library/2007.11.powershell.aspx

Powershell:使用正则表达式的多种方式 - Kevin Marquette

https://kevinmarquette.github.io/2017-07-31-Powershell-regex-regular-expression

"Hello. Yes, this is a cat." -replace 'cat','dog'
"Hello. Yes, this is a dog." -replace [regex]::Escape('.'),'!'
("Hello. Yes, this is a dog.").Replace('.','!')

PSTip –replace 操作符和 String.Replace 方法的区别

https://www.powershellmagazine.com/2012/11/12/pstip-a-difference-between-the-replace-operator-and-string-replace-method/

(Get-Content C:\test\test.txt) | 
Foreach-Object {$_ -replace "(?i)\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b",'<$0>'} | 
Set-Content C:\test\test.txt

Powershell Regex find emails and replace email with (<email>)

$txt='<p class=FillText><a name="InternetMail_P3"></a>First.Last@company-name.com</p>'
$re="[a-z0-9!#\$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#\$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
[regex]::MAtch($txt, $re, "IgnoreCase ")

Using Regex in Powershell to grab email

【讨论】:

  • $CSV1 = Import-Csv "c:\testscripts\adstudents.csv" $CSV2 = Import-Csv "c:\testscripts\studentsnospaces.csv" $CSV1 | ForEach-Object {$StudentID = $_.Student_id, $Email = $_.Student_email}
  • $CSV2 | Where-Object { $_.StudentID -eq $StudentID } |选择对象 School_id, Student_id, Student_number, State_id, Last_name, Middle_name, First_name, Grade, Gender, DOB, Race, Hispanic_Latino, Ell_status, Frl_status, IEP_status, Student_street, Student_city, Student_state, Student_zip, @{n='Student_email';e ={ $Email }}, Contact_relationship, Contact_type, Contact_name, Contact_phone, Contact_email, 用户名, 密码
  • 导出-Csv "c:\testscripts\NewFile.csv"
  • 尝试匹配两个文件中的 Student_id(它们是唯一的)并将 csv2 中的 Student_email 值替换为 csv1 中的 Student_email 值。
猜你喜欢
  • 2014-02-26
  • 2015-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
  • 2022-01-22
相关资源
最近更新 更多