我正在尝试将文件中的几组线组合成CSV线(这些组之间用一条空行隔开)。我刚刚为这个例子多次复制了这些行,但实际数据文件中每个组的信息都不同。
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
上面粘贴了输出,我怎么能让它像下面这样?
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
我尝试使用here中的解决方案,但我很难将其应用到我的示例中。
答案 0 :(得分:1)
整个文件读取,将其拆分为2个或更多连续换行符,删除空元素,然后用逗号替换剩余的换行符并将所有内容写回文件。
(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'
如果您的PowerShell版本不支持Get-Content -Raw
通过Get-Content
管道Out-String
输出:
(Get-Content 'C:\path\to\input.txt' | Out-String) ...