通过cmd改变eol角色

时间:2013-05-12 12:03:32

标签: cmd eol

我在unix eol中有一个文件file1, 我有一个脚本在其中进行一些编辑,但编辑完成到output.txt并重命名为file1,这将eol更改为windows / dos

给出了代码

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

如果没有用户输入,可以通过cmd将其更改回来或保留unix eol吗?

尝试直接输入到file1,给出一个0 kb的文件 尝试type output.txt>file1给了dos / win eol 回显除空行echo.>file1以外的任何内容改变了eol char

3 个答案:

答案 0 :(得分:2)

试试这个:

@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

答案 1 :(得分:1)

这是一个解决方案:

@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

答案 2 :(得分:1)

使用纯CMD我说不太可能。

GNU SED应该能够进行这种转变。

sed -b s/\r//g infile >outfile