Reputation: 3162
I am trying to combine several groups of lines in a file into CSV lines (the groups being separated by one blank line in between each other). I've just copied the lines several times for this example but the information would differ for each group in the actual data files.
IM123456 01/20/16 09:44:01 Reassignment PZSR7Z Reassignment from xxx-xxx-xxx-xxx to xxx-xxx-xxx-xxx-xxx-xxx-xxx IM123456 01/20/16 09:44:01 Reassignment PZSR7Z Reassignment from xxx-xxx-xxx-xxx to xxx-xxx-xxx-xxx-xxx-xxx-xxx IM123456 01/20/16 09:44:01 Reassignment PZSR7Z Reassignment from xxx-xxx-xxx-xxx to xxx-xxx-xxx-xxx-xxx-xxx-xxx IM123456 01/20/16 09:44:01 Reassignment PZSR7Z Reassignment from xxx-xxx-xxx-xxx to xxx-xxx-xxx-xxx-xxx-xxx-xxx IM123456 01/20/16 09:44:01 Reassignment PZSR7Z Reassignment from xxx-xxx-xxx-xxx to xxx-xxx-xxx-xxx-xxx-xxx-xxx
With the output pasted above, how could I get it to resemble the following?
IM123456,01/20/16 09:44:01,Reassignment,PZSR7Z,Reassignment from xxx-xxx-xxx-xxx to xxx-xxx-xxx-xxx-xxx-xxx-xxx IM123456,01/20/16 09:44:01,Reassignment,PZSR7Z,Reassignment from xxx-xxx-xxx-xxx to xxx-xxx-xxx-xxx-xxx-xxx-xxx IM123456,01/20/16 09:44:01,Reassignment,PZSR7Z,Reassignment from xxx-xxx-xxx-xxx to xxx-xxx-xxx-xxx-xxx-xxx-xxx IM123456,01/20/16 09:44:01,Reassignment,PZSR7Z,Reassignment from xxx-xxx-xxx-xxx to xxx-xxx-xxx-xxx-xxx-xxx-xxx IM123456,01/20/16 09:44:01,Reassignment,PZSR7Z,Reassignment from xxx-xxx-xxx-xxx to xxx-xxx-xxx-xxx-xxx-xxx-xxx
I attempted to utilize the solution from here, but I had a difficult time applying it to my example.
Upvotes: 0
Views: 79
Reputation: 200283
Read the file as a whole, split it at 2 or more consecutive line breaks, remove empty elements, then replace the remaining line breaks with commas and write everything back to a file.
(Get-Content 'C:\path\to\input.txt' -Raw) -split ' *(\r\n){2,}' |
Where-Object { $_.Trim() } |
ForEach-Object { $_ -replace ' *\r\n', ',' } |
Set-Content 'C:\path\to\output.csv'
If your PowerShell version doesn't support Get-Content -Raw
pipe the Get-Content
output through Out-String
:
(Get-Content 'C:\path\to\input.txt' | Out-String) ...
Upvotes: 1