0

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

3 Answers3

2

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
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • nope, still dos format. when i click on show eol char, it shows set lf=^ CRLF not LF. even tried making it LF by using unix format still doenst work – Nisham Mohammed May 12 '13 at 16:33
  • No all lines get **only** `LF` on the end, look inside with a hex editor. Please note the two empty lines, if they aren't empty this **doesn't work**. – Endoro May 12 '13 at 16:50
1

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
foxidrive
  • 40,353
  • 10
  • 53
  • 68
1

Using pure CMD I'd say unlikely.

GNU SED should be capable of this transformation though.

sed -b s/\r//g infile >outfile
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • gnu sed is not portable, is it? if not any portable solutions? – Nisham Mohammed May 12 '13 at 14:13
  • copied the same line, but the eol is not changed – Nisham Mohammed May 12 '13 at 14:16
  • GnuSED can't do it. VBS should be able to open a file and write the lines with a LF and otherwise no line ending. – foxidrive May 12 '13 at 14:20
  • Hmm - revised command worked for me. What do you mean by "portable"? It's a freely-available utility. `GNU SED` is the DOS flavour. Ux has its own version. What platforms are you targeting that are likely to run CMD on which `GNU SED` can' run? – Magoo May 12 '13 at 15:06