Powershell-将.txt数据输出到CSV

时间:2018-07-06 11:23:15

标签: powershell csv export-to-excel export-to-csv

我希望您能帮助PowerShell初学者将数据从.txt文件输出到CSV。

注意事项:

  • 每个文本文件包含多个需要提取的条目
  • 端子始终为A,后跟3位数字
  • 查询者始终以C开头,但长度可以不同
  • 文件中有两个“出生日期”格式
  • txt中的空格不是制表符分隔的,也不是全部相同的间距量。

这是我想要的CSV输出(抱歉,我没有足够的代表来上传图片!)

+------------+------+----------+----------+-------------------+----------+
| Date       | Time | Terminal | Enquirer | Enquiry           | DOB      |
+------------+------+----------+----------+-------------------+----------+
| 29/05/2017 | 1227 | A999     | CA75849  | DOE/JOHN          |  / /1988 |
+------------+------+----------+----------+-------------------+----------+
| 29/05/2017 | 1424 | A999     | CA75849  | SMITH/SIMON/PETER |  / /1967 |
+------------+------+----------+----------+-------------------+----------+
|            |      |          |          |                   |          |
+------------+------+----------+----------+-------------------+----------+

数据.txt文件示例

START                 TERMINAL    USER        ENQUIRER                  TERMINAL IP

========================================================================================================================

29/05/17 1227       A999        CA75849       8875849 OCBA NCPS RBC/12/1960



        SEARCH REF                      NAME : DOE/JOHN PAGE CODE =  

                                    DATE OF BIRTH :   /  /1988




========================================================================================================================
29/05/17 1424       A999        CA75849       8875849 OCBA NCPS RBC/60/2111                                

        SEARCH REF                        NAME : SMITH/SIMON/PETER CHAPTER CODE =  

                                    DATE OF BIRTH :   /  /1967


========================================================================================================================

请不要晕倒我的废话Powershell脚本。

我设法获得的最好的信息是在单元格中有正确的信息,但有'System.Object []',而不是实际的结果字符串。

$XmlDocument = Get-Content -Path "C:\Script Projects\Log\880006.txt"
$TodaysCSV = "C:\Script Projects\Log\880006.csv"

$TopLine = $XmlDocument | Select-String "A1" 
$Enquiry = $XmlDocument | Select-String "Search"
$DOB = $XmlDocument | Select-String "BIRTH"

$toptop = $topline -split " "

$Date = $toptop | Select-String -Pattern "^\d{2}/\d{2}/\d{2}$"
$Time = $toptop | Select-String -Pattern "^\d{4}$"
$Terminal = $toptop | Select-String -Pattern "^A\d{3}$"
$Enquirer = $toptop | Select-String -Pattern "C\w\d{5}" 

$csv = New-Object -TypeName PSObject 

Add-Member -InputObject $csv -MemberType NoteProperty -Name "Date"  -Value $Date
Add-Member -InputObject $csv -MemberType NoteProperty -Name "Time"  -Value $Time
Add-Member -InputObject $csv -MemberType NoteProperty -Name "Terminal"  -value $Terminal
Add-Member -InputObject $csv -MemberType NoteProperty -Name "Enquirer"  -value $Enquirer
Add-Member -InputObject $csv -MemberType NoteProperty -Name "Enquiry"  -value $Enquiry
Add-Member -InputObject $csv -MemberType NoteProperty -Name "DOB"  -value $DOB


$csv | Export-Csv $TodaysCSV -NoTypeInformation

我得到这个输出

    +-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+
| Date            | Time            | Terminal        | Enquirer        | Enquiry         | DOB             |
+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+
| System.Object[] | System.Object[] | System.Object[] | System.Object[] | System.Object[] | System.Object[] |
+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+
|                 |                 |                 |                 |                 |                 |
+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+
|                 |                 |                 |                 |                 |                 |
+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+

我也尝试过这种方法,但是我只能得到日期或在外面玩耍,但只能在单列中获得所有数据

$Total2 = "$date2", "$time2", "$Terminal2", "$Enquirer", "$Enquiry" 

$Columns = "Date", "Time", "Terminal", "Enquirer", "Enquiry"

$total2 | ConvertFrom-Csv -Header $Columns| export-csv $TodaysCSV -NoTypeInformation

但是我不记得我是怎么做的,但是如果我从该文件导入回PowerShell并导出为CSV格式,仍然不会让我水平显示它。

我设法得到的最好的是

Date    
----    
29/05/17
29/05/17




Time
----
1227
1424




Terminal
--------
A999    
A999    




Enquirer
--------
CA75849 
CA75849 




Enquiry                                                                         
-------                                                                         
SEARCH REF                      NAME : DOE/JOHN CHAPTER CODE =        
SEARCH REF                      NAME : SMITH/SIMON/PETER CHAPTER CODE = 

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:4)

我使用了另一种方法

  • 将文件拆分为用虚线和
  • 分隔的部分
  • 使用命名的(捕获组)3种不同的RegEx,以grep每种线型的属性。
    第一个位于RegEx101.com
  • 所有找到的属性都存储在中间的$ Row对象中,并且如果所有RegEx匹配项都聚集在$Csv

## Q:\Test\2018\07\06\SO_51209341.ps1
$FileIn   = '.\SO_51209341_data.txt'
$TodayCsv = '.\SO_51209341_data.csv'

$RE1 = [RegEx]'(?m)(?<Date>\d{2}\/\d{2}\/\d{2}) (?<Time>\d{4}) +(?<Terminal>A\d{3}) +(?<User>C[A-Z0-9]+) +(?<Enquirer>.*)$'
$RE2 = [RegEx]'\s+SEARCH REF\s+NAME : (?<Enquiry>.+?) (PAGE|CHAPTER) CODE ='
$RE3 = [RegEx]'\s+DATE OF BIRTH : (?<DOB>[0-9 /]+?/\d{4})'

$Sections = (Get-Content $FileIn -Raw) -split "={30,}`r?`n" -ne ''

$Csv = ForEach($Section in $Sections){
    $Row= @{} | Select-Object Date,Time,Terminal,User,Enquirer,Enquiry,DOB
    $Cnt = 0
    If ($Section -match $RE1){++$Cnt
        $Row.Date     = $Matches.Date
        $Row.Time     = $Matches.Time
        $Row.Terminal = $Matches.Terminal
        $Row.User     = $Matches.User
        $Row.Enquirer = $Matches.Enquirer.Trim()
    }
    If ($Section -match $RE2){++$Cnt
        $Row.Enquiry  = $Matches.Enquiry
    }
    If ($Section -match $RE3){++$Cnt
        $Row.DOB      = $Matches.DOB
    }
    if ($Cnt -eq 3){$Row}
}

$csv | Format-Table
$csv | Export-Csv $Todaycsv -NoTypeInformation

示例输出修订版

> . Q:\Test\2018\07\06\SO_51209341.ps1

Date     Time Terminal User    Enquirer                      Enquiry           DOB
----     ---- -------- ----    --------                      -------           ---
29/05/17 1227 A999     CA75849 8875849 OCBA NCPS RBC/12/1960 DOE/JOHN            /  /1988
29/05/17 1424 A999     CA75849 8875849 OCBA NCPS RBC/60/2111 SMITH/SIMON/PETER   /  /1967

答案 1 :(得分:2)

这里的问题是导出之前需要将对象转换为字符串。

要使代码正常工作,您可以略微修改对象的创建:

$csv = @()
for ($i = 0;$i -lt $Date.Length; $i++) {
$obj = New-Object -TypeName PSObject 
  Add-Member -InputObject $obj -MemberType NoteProperty -Name "Date"  -Value $Date[$i]
  Add-Member -InputObject $obj -MemberType NoteProperty -Name "Time"  -Value $Time[$i]
  Add-Member -InputObject $obj -MemberType NoteProperty -Name "Terminal"  -value $Terminal[$i]
  Add-Member -InputObject $obj -MemberType NoteProperty -Name "Enquirer"  -value $Enquirer[$i]
  Add-Member -InputObject $obj -MemberType NoteProperty -Name "Enquiry"  -value $Enquiry[$i]
  Add-Member -InputObject $obj -MemberType NoteProperty -Name "DOB"  -value $DOB[$i]
  $csv += $obj
}

说明:

这里的问题是,当您应该创建对象数组时,尝试创建一个数组对象。这就是为什么在导出时得到System.Object[]而不是期望值的原因。

注意:看看您在此处粘贴的代码和文件格式。这行:

$TopLine = $XmlDocument | Select-String "A1" 

应该是

$TopLine = $XmlDocument | Select-String "A999"