Remove Carriage Return from String Field in File Windows Command Line

2.2k views Asked by At

I have a file that is ouput with | as the delimiter. However, one of the fields is a description field from the source system which contains carriage returns. This is an issue when trying to read the file as it breaks it on to a new line. What I would like to do is remove all CF/LF that aren't preceeded by a |.

I feel like this should be possible on command line but haven't been able to come up with it.

Sample data

|A|Testing CF/LF
This|CF/LF

Expected Output

|A|Testing This|CF/LF
1

There are 1 answers

0
Magoo On
@ECHO Off
SETLOCAL
SET "line="
(
FOR /f "delims=" %%a IN (q26895698.txt) DO (
 CALL :generate "%%a"
)
)>newfile.txt

TYPE newfile.txt

GOTO :EOF

:generate
SET "line=%line%%~1"
IF "%line:~-1%"=="|" SET "line="&FOR %%x IN ("%line%") DO ECHO(%%~x
GOTO :eof

I used a file named q26895698.txt containing your data (such as it was) for my testing.

Produces newfile.txt

Can't help but get the impression that you'd be better off with SED thouh - google "GNU SED" for details...