1

I have a text file where I need to remove the carriage return/line feed in a particular situation. I don't want to remove them all.

I know that I can run code to remove CR/LF. However, I have a file where I only wish to remove the CR/LF if it is preceded by a /

The like looks like this ( inserted in correct spots ):

"2016-09-11 23:22:03","20\<CR/LF>
16-09-11 >03:22:24",20160911,1,16,21,281,281,4272,4272,NULL,NULL,NULL,0,2100,2528,NULL<CR/LF>

So I do not want the last removed, only where it is preceded by the \, is in the first example above.

Thanks in advance

Joey
  • 344,408
  • 85
  • 689
  • 683
Pat
  • 15
  • 5
  • Thanks Joey....I was working on editing and you beat me to it. :) – Pat Dec 06 '16 at 14:20
  • Is `NULL` the string `NULL` or the character `NULL` ? Can more than one lines in succession end in `/` - or is it "\"? The obvious candidate for this job is `sed` or `(g)awk` – Magoo Dec 06 '16 at 14:23
  • Magoo, there are over 1,000 lines in the file. But there is only one "slash-carriage return-line feed" on each line. Never multiples. I wish to have a repeatable process for whenever the file is produced. Thanks. – Pat Dec 06 '16 at 14:29
  • 1
    If the file is under 100k lines, I'd do this with Notepad++ Find&Replace (regex) – 9dan Dec 06 '16 at 14:49
  • Hi 9dan, I thought of that, except that I cannot place a \ in front of the regex. Basically only 1/2 of the cr/lf will be replaced. There are two per "line". One that breaks the line in half, and one at the end of the line. I only want to remove the one that breaks the line in half, and that is preceded by a slash. – Pat Dec 06 '16 at 15:11
  • See [this answer](http://stackoverflow.com/questions/40803502/echo-text-with-unix-line-endings-from-a-windows-batch-bat-script/40810929#40810929) – Aacini Dec 06 '16 at 15:34
  • How long are the final lines? more than ~ 8190 characters/bytes? – aschipfl Dec 06 '16 at 23:36
  • Using [JREPL.BAT](http://www.dostips.com/forum/viewtopic.php?t=6044) - `jrepl "\\\r\n" "" /m /f yourFile.txt /o -` – dbenham Dec 07 '16 at 13:16

2 Answers2

1

Here is a pure (well commented) solution -- supposing that the finally concatenated lines are not longer than about 8190 characters or bytes:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_FILE=%~1" & rem // (first command line argument specifies the input text file)
set "_CHAR=/"   & rem // (character that marks line concatenation)

rem // Reset buffer for concatenation:
set "CONC="
rem /* Read input text file line by line; since `for /F` ignores empty lines, use `findstr`
rem    to prefix them by their line numbers and a colon, so they do not appear empty: */
for /F "delims=" %%L in ('findstr /N "^" "%_FILE%"') do (
    rem // Store current line, including line number prefix:
    set "LINE=%%L"
    rem // Toggle delayed expansion to avoid loss of exclamation marks:
    setlocal EnableDelayedExpansion
    rem // Check whether last character is the predefined concatenation mark:
    if "!LINE:~-1!"=="%_CHAR%" (
        rem // Contatenation required, so remove line number prefix and concatenate:
        set "LINE=!LINE:*:=!"
        set "CONC=!CONC!!LINE:~,-1!"
    ) else (
        rem /* No concatenation needed, so output current line with line number prefix
        rem    removed and prefixed by current concatenation buffer: */
        echo(!CONC!!LINE:*:=!
        rem // Reset concatenation buffer:
        set "CONC="
    )
    rem /* Transfer concatenation buffer beyond the environment localisation barrier
    rem    (this is needed because of `endlocal` and toggling delayed expansion): */
    for /F "delims=" %%K in (^""!CONC!"^") do (
        endlocal
        set "CONC=%%~K"
    )
)
rem // Output remaining concatenation buffer, if there is something left:
if defined CONC (
    setlocal EnableDelayedExpansion
    echo(!CONC!
    endlocal
)

endlocal
exit /B

This uses / as the marker character. To change it to \, go to the block introduced by the comment Define constants here: on top of the script.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
0

Per suggestion from 9dan, I did this via Notepad++ in three steps.

  1. Changed all cr/lf to Xx via regex
  2. Changed \Xx to "" (blank) via regular
  3. Changed Xx to cr/lf via regex

It worked as expected.

Not certain how to accept 9dan's answer, just wanted to share.

Pat
  • 15
  • 5
  • 2
    Didn't you saw [my solution](http://stackoverflow.com/questions/40803502/echo-text-with-unix-line-endings-from-a-windows-batch-bat-script/40810929#40810929)? You just need to change this part: `.replace(/\\\r\n/g," "));` – Aacini Dec 06 '16 at 18:55