How to remove CR/LF in between the line

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
aaksar
Posts: 105
Joined: 17 Nov 2012 05:13

How to remove CR/LF in between the line

#1 Post by aaksar » 05 Mar 2013 00:05

Hi,

I am getting input file , but it contains extra CR/LF in b/w the line.

after 1 line CR/LF its fine, but when it comes in b/w the line s , it will start new lines from there only.

how can i remove the extra cr/lf
pls help me

Thanks

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: How to remove CR/LF in between the line

#2 Post by mfm4aa » 05 Mar 2013 01:55

Try this on the command line:

Code: Select all

>output.txt (for /f "tokens=*" %i in (input.txt) do @echo.%i)

aaksar
Posts: 105
Joined: 17 Nov 2012 05:13

Re: How to remove CR/LF in between the line

#3 Post by aaksar » 05 Mar 2013 03:23

Hi ,

Its removing the CRLF but if my line contain 2 CRLF its removing 1, it should remove CRLF which are comin in the middle of line, and Its removing CRLF which are coming End of the line also

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: How to remove CR/LF in between the line

#4 Post by mfm4aa » 05 Mar 2013 03:46

CR/LF in the middle of a line? Please post such a line.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: How to remove CR/LF in between the line

#5 Post by jeb » 05 Mar 2013 04:01

mfm4aa wrote:CR/LF in the middle of a line? Please post such a line.
:o

It's impossible to have two CR/LF in one line :!:
As a line is defined as text terminated by a CR/LF.
So the next CR/LF is part of the next line

jeb

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to remove CR/LF in between the line

#6 Post by foxidrive » 05 Mar 2013 04:30

I suspect he's using WMIC and it's a CR alone that is causing the issue.

aaksar, show us the command that is causing the issue...

aaksar
Posts: 105
Joined: 17 Nov 2012 05:13

Re: How to remove CR/LF in between the line

#7 Post by aaksar » 05 Mar 2013 05:46

Actually Its coming in 1 Line,

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to remove CR/LF in between the line

#8 Post by foxidrive » 05 Mar 2013 06:13

You aren't helping us understand what you're doing.

aaksar
Posts: 105
Joined: 17 Nov 2012 05:13

Re: How to remove CR/LF in between the line

#9 Post by aaksar » 05 Mar 2013 23:02

below is 1 line, but 3 CRLF is coming in/bw, RED color one is the 1 column but coming in 3 lines and with 2 CRLF,
"0711105","11","00000","119",2010-05-03-22.13.00.000000,"119",2012-12-12-12.13.00.000000,"M15",08/01/2008,"13",07/15/2007,07/15/2008,"E",," ",,"Prop XOL 10M x5M + 5M Swiss 41",,0,"N",0,"N","N",,"900","B5","30","46","44","4","N","P","P","D","119","119",07/15/2007,07/14/2008," ","00000","N","119"," ",,,," "," "," "," "," "," ",,0.00,0.00,0.00," "," "," ",0.00,"D"," "," ","N",,"","Wind excluded only to all 1st and 2nd tier counties from TX to SC inclusive.

Reinstatement equals 1 x 150%
"," "," "," "

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to remove CR/LF in between the line

#10 Post by foxidrive » 05 Mar 2013 23:15

How is it created? That's an invalid CSV file.

Fixing it as it being created is the best solution.

aaksar
Posts: 105
Joined: 17 Nov 2012 05:13

Re: How to remove CR/LF in between the line

#11 Post by aaksar » 07 Mar 2013 10:44

actually there is 1 description filed in the file.

and in that field user has entered value and then enter and 2nd value enter 3rd value..

its a free text field.

so that column value is coming in multiple line with CRLF. i need to remove the CRLF from that column only.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to remove CR/LF in between the line

#12 Post by Aacini » 07 Mar 2013 15:19

@aaksar:

- A file is a series of lines/records, each one terminated in CR+LF.

- If your file have several CR+LF embedded in one record, then THERE IS NO WAY to fix your problem, unless there is a way to distinguish the "real" CR+LF that terminate normal records from the additional CR+LF that comin "in the middle of the line". You should started your description with these details!

- I have assumed that your file have records with a fixed number of fields (70 in your example) and that the fields are separated by commas. The Batch file below read lines from the input file and concatenates several lines until complete 70 fields in one record. Note that this method would fail if a field in a "broken" record have a comma (even if it is enclosed in quotes).

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set numOfFields=70

set record=
set fields=1
(for /F "delims=" %%a in (inputFile.txt) do (
   set "record=!record!%%a"
   rem Count the fields in this line
   set "line=%%a"
   call :StrLen line allChars=
   set "noCommas=!line:,=!"
   call :StrLen noCommas withoutCommas=
   set /A fields+=allChars-withoutCommas
   if !fields! geq %numOfFields% (
      echo !record!
      set record=
      set fields=1
   )
)) > outputFile.txt
goto :EOF


:StrLen var len=
set "str=0!%1!"
set len=0
for /L %%A in (12,-1,0) do (
   set /A "len|=1<<%%A"
   for %%B in (!len!) do if "!str:~%%B,1!" == "" set /A "len&=~1<<%%A"
)
set %2=%len%
exit /B

Output:
Output wrote:"0711105","11","00000","119",2010-05-03-22.13.00.000000,"119",2012-12-12-12.13.00.000000,"M15",08/01/2008,"13",07/15/2007,07/15/2008,"E",," ",,"Prop XOL 10M x5M + 5M Swiss 41",,0,"N",0,"N","N",,"900","B5","30","46","44","4","N","P","P","D","119","119",07/15/2007,07/14/2008," ","00000","N","119"," ",,,," "," "," "," "," "," ",,0.00,0.00,0.00," "," "," ",0.00,"D"," "," ","N",,"","Wind excluded only to all 1st and 2nd tier counties from TX to SC inclusive.Reinstatement equals 1 x 150% "," "," "," "


Antonio

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to remove CR/LF in between the line

#13 Post by Squashman » 07 Mar 2013 16:36

I get messed up data from clients like this all the time and there is no real easy way to fix it unless it is just a single LF and not a CR\LF. If it is just a LF you could just remove all the Line Feeds from the file and then you will just have CR. You could then change the CR back to CRLF.

Most of the time this happens because whomever was doing their data entry in their order entry software decided they needed to hit the enter key a couple of times. Or they have Excel files with embedded Line Feeds in a cell because of formatting.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to remove CR/LF in between the line

#14 Post by dbenham » 07 Mar 2013 17:15

foxidrive wrote:How is it created? That's an invalid CSV file.

Fixing it as it being created is the best solution.


Actually, it is valid to embed line feeds within a CSV column as long as the column is quoted. But the majority of CSV readers fail to properly handle such records. Quote literals within a quoted column are represented as 2 consecutive quotes.


Dave Benham

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to remove CR/LF in between the line

#15 Post by foxidrive » 07 Mar 2013 18:04

dbenham wrote:it is valid to embed line feeds within a CSV column as long as the column is quoted.


LF or CR/LF ?

Post Reply