I hope you could help a PowerShell beginner in outputting data from a .txt file to a CSV.
Things to be keep in mind:
This is the CSV output I would like (Apologies I haven't got enough rep to upload a picture!)
+------------+------+----------+----------+-------------------+----------+
| 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 |
+------------+------+----------+----------+-------------------+----------+
| | | | | | |
+------------+------+----------+----------+-------------------+----------+
Example of data .txt file
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
========================================================================================================================
Please don't faint over my crap Powershell script.
The best I have managed to get is having the right information but 'System.Object[]' in the cells rather than the actual string of results.
$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
I get this output
+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+
| Date | Time | Terminal | Enquirer | Enquiry | DOB |
+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+
| System.Object[] | System.Object[] | System.Object[] | System.Object[] | System.Object[] | System.Object[] |
+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+
| | | | | | |
+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+
| | | | | | |
+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+
I have tried this way too but I only get the date or playing around I can get all the data but only in the single column
$Total2 = "$date2", "$time2", "$Terminal2", "$Enquirer", "$Enquiry"
$Columns = "Date", "Time", "Terminal", "Enquirer", "Enquiry"
$total2 | ConvertFrom-Csv -Header $Columns| export-csv $TodaysCSV -NoTypeInformation
But can't remember how I did it and still wont let me display it horizontally if I import from that file back into PowerShell and export as CSV.
The best I managed to get was this
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 =
Thank you in advance for your help!
I used a different approach,
$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
Sample output revised version
> . 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
The problem here is that you need to convert objects to string before exporting.
To make your code working you can slightly modify object creation:
$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
}
The issue here is that you try to make a object of arrays while you should create array of objects. That's why when exporting you got System.Object[]
instead of expected value.
Note : take a look at the code you pasted here and file format. This line:
$TopLine = $XmlDocument | Select-String "A1"
should be
$TopLine = $XmlDocument | Select-String "A999"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.