簡體   English   中英

將信息從txt文件提取到CSV

[英]Extract information from txt file to CSV

我的部門每周都會收到200-1000封電子郵件,以進行審核並從我們的一個應用中刪除。 該應用程序將允許我們使用CSV來“限定”用戶。 我將電子郵件導出到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   

假設您的數據看起來像您發布的:

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

確保更改了代碼中的路徑,並且上一個屬性名稱的文件中確實包含“ ”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM