Question

i have a file file1in with unix eol, i have a script to do some editing in it, but the editing is done into output.txt and is renamed as file1and this changes the eol to windows/dos

the code is given

set uu=file1
set vv=file2

setlocal EnableDelayedExpansion
(for /F "delims=" %%a in (%uu%) do (
   set "line=%%a"
   if "!line:345=!" neq "!line!" (
      if "!line:123=!" neq "!line!" (
         if not defined flag (
            findstr "123" %vv% | findstr "345"
            set flag=true
         )
      ) else (
         echo !line!
      )
   ) else (
      echo !line!
   )
)) >output.txt 
del %uu% 
rename output.txt file1

any way to change it back or retain unix eol via cmd without user input?

have tried directly inputing into the file1, gives a 0 kb file tried type output.txt>file1 gave dos/win eol echoing anything other than a blank line echo.>file1 changed the eol char

Was it helpful?

Solution 2

Here's a solution:

@echo off
::Syntax: batchfile "file.txt" >"file2.txt"

:init
for /f %%c in ('copy /z "%~dpnx0" nul') do set cr=%%c
(set lf=^

)
del file.tmp 2>nul
for /f "delims=" %%i in ('findstr /n "^" "%~1"') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:*:=!!lf!"
echo(!line!
endlocal
)>>file.tmp
setlocal enabledelayedexpansion
findstr /v "!cr!!lf!" file.tmp
endlocal
del file.tmp 2>nul

OTHER TIPS

try this:

@echo off&setlocal enabledelayedexpansion
set "uu=file1"
set "vv=file2"
set LF=^


rem keep two empty lines between set LF and here
setlocal EnableDelayedExpansion
(for /F "delims=" %%a in (%uu%) do (
   set "line=%%a"
   if "!line:345=!" neq "!line!" (
      if "!line:123=!" neq "!line!" (
         if not defined flag (
            findstr "123" %vv% | findstr "345"
            set flag=true
         )
      ) else (
         <nul set/p"=!line!!LF!"
      )
   ) else (
      <nul set/p"=!line!!LF!"
   )
)) >output.txt 
type output.txt

Using pure CMD I'd say unlikely.

GNU SED should be capable of this transformation though.

sed -b s/\r//g infile >outfile
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top