0

我有一个file1包含 unix eol 的文件,我有一个脚本可以在其中进行一些编辑,但是编辑完成并重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 字符

4

3 回答 3

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
于 2013-05-12T15:05:03.507 回答
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
于 2013-05-12T13:35:03.660 回答
1

使用纯CMD我会说不太可能。

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

sed -b s/\r//g infile >outfile
于 2013-05-12T13:57:40.660 回答