将信息从txt文件提取到CSV

时间:2016-04-27 12:10:05

标签: csv powershell

我的部门每周会收到200-1000封电子邮件,然后从我们的某个应用中删除。该应用程序将让我们使用CSV到" term"用户。我将电子邮件导出到txt,并尝试获取CSV所需的信息。

以下是我的PowerShell代码:

Select-String -Path 'C:\Users\Public\Documents\April22.txt' -Pattern "Account name:", "Name:"
$A = "Name:"; $B = "Account Name:";
$wrapper = New-Object PSObject @{ FirstColumn = $A; SecondColumn = $B }
Export-Csv -InputObject $Wrapper -Path C:\Downloads\Test.csv -NoTypeInformation

以下是txt文件信息的示例:

Account Information:
Name:    something, something    
Account Name:    something   
Email:   something@something.org     
Title:   something   
Department:  something   
Office Location:     something   

1 个答案:

答案 0 :(得分:0)

假设您的数据看起来像是您发布的:

Account Information: Name:  LastName, Fristname Account Name: test-person   Account Email:  user@web.org    Title: Info Department: dept-17287  Office Location:    location    Phone Number:   Employee Type:  Ex-employee Employee ID:    1158175 Employee End Date:  2016-04-18 07:00    Manager:    name: test-manager ;

您可以尝试这样的事情:

   Get-Content -Path 'C:\a.txt' | ForEach-Object {
    $line = $_ -split "`t"
    $obj = New-Object PSObject
    $obj | Add-Member -MemberType NoteProperty -Name 'Account Name' -Value (($line[2] -split ':')[1]).Trim()
    $obj | Add-Member -MemberType NoteProperty -Name 'name' -Value (($line[-1] -split ':' -replace ';')[1]).Trim()
    $obj
    } | Export-Csv -Path C:\test.csv -NoTypeInformation

确保您更改了代码中的路径,最后一个属性名称确实有" "在你的文件中。