Goal is to remove all single "Line Feeds"(LFs) and keep the "Line Feeds" which follow a Carriage Return(CR) in a csv
file.
I got a report which includes several LFs in one row, but I only want to keep the "CR+LF" so every row stands for one reported object.
I need a solution in PowerShell
, unfortunately I am very new to PowerShell scripting. I tried to alter some scripts on this side for this job, but its not working.
First, I would try to remove all LFs in the file, and then I would replace all remaining CRs with [CR][LF]
. But I did not achieve the first step.
$original_file ='C:\Test\Server.csv'
$new_file = 'C:\Test\Server_changed.csv'
(Get-Content $original_file -Raw).Replace('´n',' ') | Set-Content $new_file -Force
(Get-Content $new_file -Raw).Replace('`r','`r`n') | Set-Content $new_file -Force
Source CSV
:
"Servername";"CPU","Memory";"Annotation";"OperatingSystem"[CR][LF]
"Server1";"4";"8";"very importand Server!![LF]
If reboot is needed:[LF]
1. Contact Me[LF]
2. Stop all running Services before shutting down the OS[LF]
";"Windows Server 2019";[CR][LF]
How it should look:
"Servername";"CPU","Memory";"Annotation";"OperatingSystem"[CR][LF]
"Server1";"4";"8";"very importand Server!! If reboot is needed: 1. Contact Me 2. Stop all running Services before shutting down the OS ";"Windows Server 2019";[CR][LF]